Tuesday, March 22, 2011

Find all the Stored Procedures where a user has been explicitly granted or denied access (and script them)

This will list all the stored procedures where a user has been explicitly granted or denied certain access

Select State_Desc,
       Permission_Name,
       Object_Schema_Name(Major_ID) as [SP_Schema],
       Object_Name(Major_ID) as [SP_Name],
       User_Name(Grantee_Principal_ID) as [Principal_Name], *
  From sys.database_permissions PERM inner join sys.database_principals PRIN
    on PERM.Grantee_Principal_ID = PRIN.Principal_ID
 Where PERM.Class = 1 
   and PRIN.Name = '<UserName>' 
 Order by Object_Name(Major_ID)

This will generate a script that will recreate that access

Select State_Desc + ' ' + Permission_Name + ' On ['
       + Object_Schema_Name(Major_ID) +
       '].[' + Object_Name(Major_ID) + '] to ['
       + User_Name(Grantee_Principal_ID) + ']'
  From sys.database_permissions PERM inner join sys.database_principals PRIN
    on PERM.Grantee_Principal_ID = PRIN.Principal_ID
 Where PERM.Class = 1
   and PRIN.Name = '<UserName>' 
 Order by Object_Name(Major_ID)

No comments:

Post a Comment