Advertisement
Guest User

Untitled

a guest
May 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. set nocount on
  2. declare @permission table (
  3. Database_Name sysname,
  4. User_Role_Name sysname,
  5. Account_Type nvarchar(60),
  6. Action_Type nvarchar(128),
  7. Permission nvarchar(60),
  8. ObjectName sysname null,
  9. Object_Type nvarchar(60)
  10. )
  11. declare @dbs table (dbname sysname)
  12. declare @Next sysname
  13. insert into @dbs
  14. --Use this for all databases
  15. select name from sys.databases order by name
  16. --Uncomment and use this for a single DB
  17. --select name from 'YOURDBNAMEHERE' as dbname
  18. select top 1 @Next = dbname from @dbs
  19. while (@@rowcount<>0)
  20. begin
  21. insert into @permission
  22. exec('use [' + @Next + ']
  23. declare @objects table (obj_id int, obj_type char(2))
  24. insert into @objects
  25. select id, xtype from master.sys.sysobjects
  26. insert into @objects
  27. select object_id, type from sys.objects
  28.  
  29. SELECT ''' + @Next + ''', a.name as ''User or Role Name'', a.type_desc as ''Account Type'',
  30. d.permission_name as ''Type of Permission'', d.state_desc as ''State of Permission'',
  31. OBJECT_SCHEMA_NAME(d.major_id) + ''.'' + object_name(d.major_id) as ''Object Name'',
  32. case e.obj_type
  33. when ''AF'' then ''Aggregate function (CLR)''
  34. when ''C'' then ''CHECK constraint''
  35. when ''D'' then ''DEFAULT (constraint or stand-alone)''
  36. when ''F'' then ''FOREIGN KEY constraint''
  37. when ''PK'' then ''PRIMARY KEY constraint''
  38. when ''P'' then ''SQL stored procedure''
  39. when ''PC'' then ''Assembly (CLR) stored procedure''
  40. when ''FN'' then ''SQL scalar function''
  41. when ''FS'' then ''Assembly (CLR) scalar function''
  42. when ''FT'' then ''Assembly (CLR) table-valued function''
  43. when ''R'' then ''Rule (old-style, stand-alone)''
  44. when ''RF'' then ''Replication-filter-procedure''
  45. when ''S'' then ''System base table''
  46. when ''SN'' then ''Synonym''
  47. when ''SQ'' then ''Service queue''
  48. when ''TA'' then ''Assembly (CLR) DML trigger''
  49. when ''TR'' then ''SQL DML trigger''
  50. when ''IF'' then ''SQL inline table-valued function''
  51. when ''TF'' then ''SQL table-valued-function''
  52. when ''U'' then ''Table (user-defined)''
  53. when ''UQ'' then ''UNIQUE constraint''
  54. when ''V'' then ''View''
  55. when ''X'' then ''Extended stored procedure''
  56. when ''IT'' then ''Internal table''
  57. end as ''Object Type''
  58. FROM [' + @Next + '].sys.database_principals a
  59. left join [' + @Next + '].sys.database_permissions d on a.principal_id = d.grantee_principal_id
  60. left join @objects e on d.major_id = e.obj_id
  61. order by a.name, d.class_desc')
  62. delete @dbs where dbname = @Next
  63. select top 1 @Next = dbname from @dbs
  64. end
  65. set nocount off
  66. select * from @permission
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement