我正在尝试确保为所有用户分配了特定角色。是否有任何视图或SQL查询来获取此信息?
答案 0 :(得分:29)
视图已更改名称,但以下内容适用于SQL Azure
select m.name as Member, r.name as Role
from sys.database_role_members
inner join sys.database_principals m on sys.database_role_members.member_principal_id = m.principal_id
inner join sys.database_principals r on sys.database_role_members.role_principal_id = r.principal_id
答案 1 :(得分:-4)
我认为下面列出的查询应该列出单个数据库的用户和角色
select b.name as USERName, c.name as RoleName from dbo.sysmembers a join dbo.sysusers b on a.memberuid = b.uid join dbo.sysusers c on a.groupuid = c.uid
或者对于SQL实例上的所有DB,您可以使用sp_MSForEachDB
Exec dbo.sp_MSForEachDB 'select ''?'' as DB, b.name as USERName, c.name as RoleName from dbo.sysmembers a join dbo.sysusers b on a.memberuid = b.uid join dbo.sysusers c on a.groupuid = c.uid'