List of Database Users with Database Roles

  • HI, I saw this great procedure.

    My requirement is similar but I have around 50 instances.

    Can we change this procedure to get a instance name as an argument and not compliling this procedure to each machine and I can get the information from centralized machine?

  • Just on spot...but it failed on dabase with spaces in between like [my fun database]

    “When I hear somebody sigh, ‘Life is hard,’ I am always tempted to ask, ‘Compared to what?’” - Sydney Harris

  • How add serverole by user? this is very important

  • --#### LOGINS,USERS,ROLES (LUR) SCRIPT ####

    --#### SCRIPT TESTED IN SQL SERVER 2000,2005,2008 AND 2008R2####

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    -- CREATING A TEMP TABLE TO LOAD WITH DATABASE ROLES

    CREATE TABLE [TEMPDB].[DBO].[DB_ROLES](

    [DBNAME] [SYSNAME] ,

    [USERNAME] [SYSNAME] ,

    [DB_OWNER] [VARCHAR](3) ,

    [DB_ACCESSADMIN] [VARCHAR](3) ,

    [DB_SECURITYADMIN] [VARCHAR](3) ,

    [DB_DDLADMIN] [VARCHAR](3) ,

    [DB_DATAREADER] [VARCHAR](3) ,

    [DB_DATAWRITER] [VARCHAR](3) ,

    [DB_DENYDATAREADER] [VARCHAR](3) ,

    [DB_DENYDATAWRITER] [VARCHAR](3) ,

    [DT_CREATE] [DATETIME] NOT NULL,

    [DT_UPDATE] [DATETIME] NOT NULL,

    [DT_REPORT] [DATETIME] NOT NULL CONSTRAINT [DF__DBROLES__CUR_DAT__3A179ED3] DEFAULT (GETDATE())

    ) ON [PRIMARY]

    GO

    INSERT INTO [TEMPDB].[DBO].[DB_ROLES]

    EXEC SP_MSFOREACHDB

    'SELECT

    ''?'' AS DBNAME,

    USERNAME,

    MAX(CASE ROLENAME WHEN ''DB_OWNER'' THEN ''YES'' ELSE ''NO'' END) AS DB_OWNER,

    MAX(CASE ROLENAME WHEN ''DB_ACCESSADMIN '' THEN ''YES'' ELSE ''NO'' END) AS DB_ACCESSADMIN ,

    MAX(CASE ROLENAME WHEN ''DB_SECURITYADMIN'' THEN ''YES'' ELSE ''NO'' END) AS DB_SECURITYADMIN,

    MAX(CASE ROLENAME WHEN ''DB_DDLADMIN'' THEN ''YES'' ELSE ''NO'' END) AS DB_DDLADMIN,

    MAX(CASE ROLENAME WHEN ''DB_DATAREADER'' THEN ''YES'' ELSE ''NO'' END) AS DB_DATAREADER,

    MAX(CASE ROLENAME WHEN ''DB_DATAWRITER'' THEN ''YES'' ELSE ''NO'' END) AS DB_DATAWRITER,

    MAX(CASE ROLENAME WHEN ''DB_DENYDATAREADER'' THEN ''YES'' ELSE ''NO'' END) AS DB_DENYDATAREADER,

    MAX(CASE ROLENAME WHEN ''DB_DENYDATAWRITER'' THEN ''YES'' ELSE ''NO'' END) AS DB_DENYDATAWRITER,

    CREATEDATE,

    UPDATEDATE,

    GETDATE()

    FROM (

    SELECT B.NAME AS USERNAME, C.NAME AS ROLENAME, B.CREATEDATE, B.UPDATEDATE

    FROM

    [?].DBO.SYSMEMBERS A JOIN [?].DBO.SYSUSERS B ON A.MEMBERUID = B.UID

    JOIN [?].DBO.SYSUSERS C ON A.GROUPUID = C.UID

    )S

    GROUP BY USERNAME, CREATEDATE, UPDATEDATE

    ORDER BY USERNAME'

    -- RETRIVE LOGINS, USERS AND ROLES TOGETHER

    SELECT SERVERPROPERTY('SERVERNAME') AS [SERVERNAME],

    B.NAME AS [LOGINNAME],

    CASE B.SYSADMIN WHEN '1' THEN 'YES' ELSE 'NO' END AS SYSADMIN,

    CASE B.SECURITYADMIN WHEN '1' THEN 'YES' ELSE 'NO' END AS SECURITYADMIN,

    CASE B.SETUPADMIN WHEN '1' THEN 'YES' ELSE 'NO' END AS SETUPADMIN,

    CASE B.PROCESSADMIN WHEN '1' THEN 'YES' ELSE 'NO' END AS PROCESSADMIN,

    CASE B.DISKADMIN WHEN '1' THEN 'YES' ELSE 'NO' END AS DISKADMIN,

    CASE B.DBCREATOR WHEN '1' THEN 'YES' ELSE 'NO' END AS DBCREATOR,

    CASE B.BULKADMIN WHEN '1' THEN 'YES' ELSE 'NO' END AS BULKADMIN,

    B.DBNAME AS [DEFAULT_DBNAME],

    A.* INTO #LOGINS FROM [TEMPDB].[DBO].[DB_ROLES] A RIGHT JOIN MASTER..SYSLOGINS B ON A.USERNAME=B.NAME

    --WHERE B.ISNTUSER=1 --INCLUDE TO EXCLUDE THE SQL LOGINS

    SELECT * FROM #LOGINS ORDER BY [LOGINNAME]

    DROP TABLE [TEMPDB].[DBO].[DB_ROLES]

    DROP TABLE #LOGINS

    --------------SCRIPT END-----------------------

    [font="Tahoma"]
    --SQLFRNDZ[/url]
    [/font]

  • Great script and some interesting procedures posted I was not aware of.

    Whilst this seems to do a fair job of auditing all users in the current instance of the server, I think it would be handy to have something that could audit all SQL servers on the network. Therefore adding an additional column with SERVER\Instance and somehow making this script refer to a table of all SERVER\Instances

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply