Friday, February 24, 2012

Grant Execute Permission to User for SP & Functions

--Grants EXECUTE permission to TestUser for all user created procedures.

declare @username varchar(255)
set @username = 'TestUser'
SELECT 'grant exec on ' + QUOTENAME(ROUTINE_SCHEMA) + '.' +
QUOTENAME(ROUTINE_NAME) + ' TO ' + @username FROM INFORMATION_SCHEMA.ROUTINES
WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),'IsMSShipped') = 0
and ROUTINE_TYPE='PROCEDURE'

-- Grants EXECUTE permission to TestUser for all user created functions.

declare @username varchar(255)
set @username = 'TestUser'
SELECT 'grant exec on ' + QUOTENAME(ROUTINE_SCHEMA) + '.' +
QUOTENAME(ROUTINE_NAME) + ' TO ' + @username FROM INFORMATION_SCHEMA.ROUTINES
WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME),'IsMSShipped') = 0
and ROUTINE_TYPE='FUNCTION'

Friday, February 10, 2012

Backup and Restore History of a Particular DB

-- Backup History

SELECT s.backup_set_id,s.user_name,s.backup_start_date,s.backup_finish_date,
datediff(mi,s.backup_start_date,s.backup_finish_date)as mins,
datediff(s,s.backup_start_date,s.backup_finish_date)as sec,
s.type,s.backup_size,s.database_name,f.physical_device_name,s.server_name,
s.recovery_model
from msdb..backupset s,msdb..backupmediafamily f
where s.media_set_id=f.media_set_id --and s.database_name='{Database name}'
order by s.backup_finish_date DESC


-- Restore History

SELECT h.backup_set_id,h.destination_database_name,f.physical_device_name,
h.restore_date ,h.user_name,h.restore_type,h.replace,s.server_name
from msdb..restorehistory h,msdb..backupmediafamily f,msdb..backupset s
where h.backup_set_id=s.backup_set_id and s.media_set_id=f.media_set_id
--and h.destination_database_name ='{Database name}'
ORDER BY h.restore_date DESC