我想获得具有指定角色的所有数据库用户的列表。 Role是存储过程或函数的参数。
Somethinf喜欢带有用户名及其角色的select语句。
+============+==========
| User name | Role |
+============+==========
MS SQL Server 2008
答案 0 :(得分:32)
在SQL 2005和2008中,可以在两个目录视图中轻松访问此信息。
此查询应该为您提供所需的信息。
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)
答案 1 :(得分:3)
仅取决于您希望如何传递参数...假设您将使用角色的ID
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
答案 2 :(得分:2)
可能使用类似的东西
SELECT
SU.Name AS UserName, SR.Name AS RoleName
FROM
sysUsers AS SU
INNER JOIN
sysUsers AS SR ON SU.GID = SR.UID
WHERE
SU.GID <> SU.UID
ORDER BY
RoleName, UserName
编辑1 获取与用户关联的系统角色。 sys.sysmembers是一个系统视图,其中memberuid和groupuid是唯一的列。您可以使用user_name()函数来检索每列的名称。
USE [YourDatabase]
SELECT user_name([memberuid]) as [Username], User_Name([groupuid]) as [Role_Name]
FROM [sys].[sysmembers]
答案 3 :(得分:0)
类似的事情可以为您提供帮助:
select sys.server_role_members.role_principal_id ,role.name as rolename
, sys.server_role_members.member_principal_id , member.name as membername
from sys.server_role_members
join sys.server_principals as role
on sys.server_role_members.role_principal_id = role.principal_id
join sys.server_principals as member
on sys.server_role_members. member_principal_id = member_principal_id;