Get list of all database users with specified role

前端 未结 4 1572
情书的邮戳
情书的邮戳 2021-01-01 11:44

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

4条回答
  •  天涯浪人
    2021-01-01 12:04

    Probably use something like this

    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
    

    Borrowed from SmartBihari's Blog

    EDIT 1 To Get the System Roles associated with a user. The sys.sysmembers is a system view which has the memberuid and the groupuid as the only columns. you can use the user_name() function to retreive the name of each column.

    USE [YourDatabase]
    SELECT user_name([memberuid]) as [Username], User_Name([groupuid]) as [Role_Name]
    FROM [sys].[sysmembers]
    

提交回复
热议问题