what is the query statement to get list of all Users within a specific DB role?

techresearch7777777 1,776 Reputation points
2023-03-21T21:36:22.0266667+00:00

Hello, what is the query statement to get list of all Users within a specific DB role?

Thanks in advance.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,708 questions
{count} votes

Accepted answer
  1. Olaf Helper 40,816 Reputation points
    2023-03-22T07:06:23.5466667+00:00

    within a specific DB role?

    For database roles & members you have to use the database DMV instead of server DMV's =>

    select *
    from sys.database_principals as roles
         inner join
         sys.database_role_members as drm
             on roles.principal_id = drm.role_principal_id
         inner join
         sys.database_principals as mem
             on drm.member_principal_id = mem.principal_id
    where roles.type = 'R'
    

1 additional answer

Sort by: Most helpful
  1. Bjoern Peters 8,781 Reputation points
    2023-03-22T01:08:27.0933333+00:00

    Hi techresearch,

    SELECT m.name as Principal, r.name as Role
    FROM
    	master.sys.server_role_members rm 
    		INNER JOIN master.sys.server_principals r 
    			ON r.principal_id = rm.role_principal_id AND r.type = 'R'
    		INNER JOIN master.sys.server_principals m
    			ON m.principal_id = rm.member_principal_id
    WHERE m.name = 'testuser'
    

    I hope my answer is helpful to you,

    Your

    Bjoern Peters

    If the reply was helpful, please upvote and/or accept it as an answer, as this helps others in the community with similar questions. Thanks!

    1 person found this answer helpful.
    0 comments No comments