I’m not clear about the security-related catalog views in SQL Server 2005 or 2008. I want to list all logins, their server roles, their correspond users in all database, all database roles in one query. How can I write the query?
I know there are some catalog views to use, but I’m not familiar with their relation. These catalog views include: sys.database_role_member, sys.database_principals, sys.server_role_member, sys.server_principals.
Thanks.
You cannot have one query list all databases because the list is dynamic. Your best bet is to use
sp_msforeachdband have a batch construct the result and return it:You can extend this to include the server roles and database roles memberships once you figure out a proper result set shape to aggregate all that information in a single table.