Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of all database users with specified role

Tags:

tsql

I want to get list of all database users with specified role. Role is a parameter for stored procedure or function.

Somethinf like a select statement with user name and its role.

+============+========== | User name  |  Role   | +============+========== 

MS SQL Server 2008

like image 556
nik Avatar asked Jul 29 '10 11:07

nik


People also ask

How can I get a list of all database users?

Answer: In SQL Server, there is a system view called sys. database_principals. You can run a query against this system view that returns all of the Users that have been created in SQL Server as well as information about these Users.

How do I get a list of user roles in SQL Server?

To find all the role assignments to users in SQL Server database, you can use the following query. SELECT r.name role_principal_name, m.name AS member_principal_name FROM sys. database_role_members rm JOIN sys. database_principals r ON rm.

How do I get list of Logins and permissions in SQL Server?

Using SQL Server Management Studio First, move to “Object Explorer” and expand the database that you want. Next, under the database, expand the “Security” directory. Now, under Security, expand the “Users” option. This will display a list that contains all the users created in that database.

How do I see user roles in Azure SQL?

Expand the Azure SQL DB and navigate to security -> Roles -> Database Roles to get a list of available fixed database roles, expand the Azure SQL DB and navigate to Security -> Roles -> Database Roles.


2 Answers

In SQL 2005 and 2008 this information is most easily accessed in two catalog views.

This query should give you the information you're looking for.

select rp.name as database_role, mp.name as database_user from sys.database_role_members drm join sys.database_principals rp on (drm.role_principal_id = rp.principal_id) join sys.database_principals mp on (drm.member_principal_id = mp.principal_id) 
like image 141
Jim V. Avatar answered Sep 19 '22 11:09

Jim V.


just depending on how you want to pass the parameter...assuming you'll use the id of the role

declare @roleID int  select     role_principal_id as roleID,      user_name(role_principal_id) as roleName,      member_principal_id as memberID,     user_name(member_principal_id) as memberName  from      sys.database_role_members where      role_principal_id = @roleID 
like image 43
dave Avatar answered Sep 19 '22 11:09

dave