Tuesday, March 20, 2012

Query to report database user role membership

Hi All,

I using sql server 2005 sp2. I'm trying to construct a query that'll show me all roles and the users that are in the roles. I'm having trouble figuring how sql server stores this information in the system tables. I think it's based around the database_principals system tables but haven't been able to display the info i need.

If anyone has a query like this can they please help me?

thanks,
Dave
I figured it out. I found the sys.database_role_members which had the info i was after:

select distinct c.name, b.name from sys.database_role_members a
inner join sys.database_principals b on b.principal_id = a.member_principal_id
inner join sys.database_principals c on c.principal_id = a.role_principal_id
and b.type <> 'R

No comments:

Post a Comment