Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

List of Database Users with Database Roles Expand / Collapse
Author
Message
Posted Wednesday, November 2, 2005 2:53 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, July 24, 2014 1:52 PM
Points: 290, Visits: 52
Interesting, though a bit long winded way to do it. Lots of good tips in this thread though.

John Scarborough
MCDBA, MCSA
Post #234678
Posted Tuesday, February 3, 2009 6:16 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, November 9, 2009 2:56 AM
Points: 2, Visits: 4
Superb!
Joining sysmembers and sysusers is a superb idea.

Just used it in a recovery from crash.

Thank you
Post #649458
Posted Thursday, March 11, 2010 1:51 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, November 1, 2013 1:06 PM
Points: 9, Visits: 161
Great stuff. This has been here a while bit if anyone is interested. Similiar code.
Slightly modified to work with SQLOverview. With it I can hit several servers. Now I get the data I am looking for a way to determine any changes. I have some ideas to determine changes or additions to any database/user/roles. Anyone done anything like this with out a trigger?

IF EXISTS 
(SELECT *
FROM tempdb.dbo.sysobjects
WHERE id = OBJECT_ID(N'[tempdb].[dbo].[DBROLES]')
)
DROP TABLE [tempdb].[dbo].[DBROLES]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [tempdb].[dbo].[DBROLES](
[ServerName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_DBROLES_ServerName] DEFAULT (N'.'),
[DBName] [sysname] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[UserName] [sysname] COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_owner] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_accessadmin] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_securityadmin] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_ddladmin] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_datareader] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_datawriter] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_denydatareader] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[db_denydatawriter] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[createdate] [datetime] NOT NULL,
[updatedate] [datetime] NOT NULL,
[Cur_Date] [datetime] NOT NULL CONSTRAINT [DF__DBROLES__Cur_Dat__3A179ED3] DEFAULT (getdate())
) ON [PRIMARY]

GO

INSERT INTO [tempdb].[dbo].[DBROLES]
EXEC sp_MSForEachDB
' SELECT CONVERT(nvarchar(128), SERVERPROPERTY(''Servername'')) AS ServerName,
''?'' 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'



SET ANSI_PADDING OFF




Post #881323
Posted Tuesday, May 4, 2010 2:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, June 16, 2010 10:35 PM
Points: 6, Visits: 23
After executing this I got a list of all users and their database access and roles. Then I realized something was missing. I used to login to sql server 2000 and execute sql statements. I am local system admin. I can see a login named Builtin\Administrator under security and this has access to all database as db_public and db_owner.

Here i have 2 questions
1. Why is Builtin\administrators not listed ? doest that mean that the procedure and query given in the link is wrong.
2. I can see certain users who has access to few of the databases. But these users are not in the login section under security. How can a user have access to database without a login ?


I would be extremely thankful to you could clarify my queries.

Thanks
Post #915133
Posted Tuesday, August 17, 2010 3:41 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Sunday, July 20, 2014 1:08 PM
Points: 537, Visits: 1,918
hi how would you run this for one login?
Post #970770
Posted Wednesday, March 16, 2011 1:26 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 22, 2013 9:55 PM
Points: 22, Visits: 124
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?
Post #1078816
Posted Thursday, September 29, 2011 7:13 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, August 18, 2014 1:24 AM
Points: 952, Visits: 1,157
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
Post #1183144
Posted Monday, December 5, 2011 7:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 5, 2011 7:50 AM
Points: 1, Visits: 2
How add serverole by user? this is very important
Post #1216316
Posted Wednesday, August 8, 2012 4:40 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, August 17, 2014 8:42 PM
Points: 463, Visits: 1,030
--#### 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-----------------------






--SQLFRNDZ
Post #1342304
Posted Friday, July 4, 2014 4:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, July 27, 2014 2:47 PM
Points: 17, Visits: 64
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
Post #1589283
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse