Below script will help you to extract a list of all the SQL Server Logins and a comma-separated list of the Server Roles they belong to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
USE [master]; GO SELECT DISTINCT rm.member_principal_id AS [Principal ID], m.name AS [Principal Name], m.type_desc AS [Principal Type], STUFF(( SELECT ',' + r_sub.name FROM sys.server_role_members rm_sub INNER JOIN sys.server_principals AS r_sub ON rm_sub.role_principal_id = r_sub.principal_id INNER JOIN sys.server_principals AS m_sub ON rm_sub.member_principal_id = m_sub.principal_id WHERE rm.member_principal_id=rm_sub.member_principal_id FOR XML PATH ('') ),1,1,'') AS [Role Name], m.default_database_name AS [Default Database], r.create_date AS [Create Date], m.modify_date AS [Modify Date] FROM sys.server_role_members rm INNER JOIN sys.server_principals AS r ON rm.role_principal_id = r.principal_id INNER JOIN sys.server_principals AS m ON rm.member_principal_id = m.principal_id ORDER BY m.name |
Leave a Comment