How to get the list of all database users

前端 未结 6 1897
感情败类
感情败类 2021-01-30 06:40

I am going to get the list of all users, including Windows users and \'sa\', who have access to a particular database in MS SQL Server. Basically, I would like the list to look

6条回答
  •  离开以前
    2021-01-30 07:02

    I try to avoid using the "SELECT * " option and just pull what data I want or need. The code below is what I use, you may cull out or add columns and aliases per your needs.

    I also us "IIF" (instant if) to replace binary 0 or 1 with a yes or no. It just makes it easier to read for the non-techie that may want this info.

    Here is what I use:

    SELECT 
        name AS 'User'
      , PRINCIPAL_ID
      , type AS 'User Type'
      , type_desc AS 'Login Type'
      , CAST(create_date AS DATE) AS 'Date Created' 
      , default_database_name AS 'Database Name'
      , IIF(is_fixed_role LIKE 0, 'No', 'Yes') AS 'Is Active'
    FROM master.sys.server_principals
    WHERE type LIKE 's' OR type LIKE 'u'
    ORDER BY [User], [Database Name]; 
    GO
    

    Hope this helps.

提交回复
热议问题