[SQL Server]ユーザーへのロール割り当て一覧

ユーザーに割り当てたロールの一覧を表示するSQL

コード

select
	mprincipals.name as member_name,
	mprincipals.type_desc as member_type,
	rprincipals.name as role_name
from sys.database_role_members as roles
inner join sys.database_principals as rprincipals on roles.role_principal_id=rprincipals.principal_id
inner join sys.database_principals as mprincipals on roles.member_principal_id=mprincipals.principal_id

2023/03/07追記 こっちのほうが簡単かも

SELECT
	user_name(role_principal_id) as [Role],
	principals.type_desc as member_type,
	user_name(member_principal_id) as [Member]
FROM sys.database_role_members as members
inner join sys.database_principals as principals on members.member_principal_id=principals.principal_id

コメントを残す