How to list role members in SQL Server 2008 R2

前端 未结 3 1598
猫巷女王i
猫巷女王i 2021-02-01 05:38

I\'m using the following T-SQL to obtain role members from my SQL Server 2008 R2 database:

select rp.name as database_role, mp.name as database_user
from sys.dat         


        
3条回答
  •  迷失自我
    2021-02-01 06:01

    Try this

    ;with ServerPermsAndRoles as
    (
        select
            spr.name as principal_name,
            spr.type_desc as principal_type,
            spm.permission_name collate SQL_Latin1_General_CP1_CI_AS as security_entity,
            'permission' as security_type,
            spm.state_desc
        from sys.server_principals spr
        inner join sys.server_permissions spm
        on spr.principal_id = spm.grantee_principal_id
        where spr.type in ('s', 'u')
    
        union all
    
        select
            sp.name as principal_name,
            sp.type_desc as principal_type,
            spr.name as security_entity,
            'role membership' as security_type,
            null as state_desc
        from sys.server_principals sp
        inner join sys.server_role_members srm
        on sp.principal_id = srm.member_principal_id
        inner join sys.server_principals spr
        on srm.role_principal_id = spr.principal_id
        where sp.type in ('s', 'u')
    )
    select *
    from ServerPermsAndRoles
    order by principal_name
    

    (Or)

    SELECT p.name, o.name, d.*
    FROM sys.database_principals AS p
    JOIN sys.database_permissions AS d ON d.grantee_principal_id = p.principal_id
    JOIN sys.objects AS o ON o.object_id = d.major_id
    

提交回复
热议问题