DECLARE @database nVARCHAR(128)=NULL
DECLARE @user VARCHAR(20)=NULL
DECLARE @dbo CHAR(1)=NULL
DECLARE @access CHAR(1)=NULL
DECLARE @security CHAR(1)=NULL
DECLARE @ddl CHAR(1)=NULL
DECLARE @datareader CHAR(1)=NULL
DECLARE @datawriter CHAR(1)=NULL
DECLARE @denyread CHAR(1)=NULL
DECLARE @denywrite CHAR(1)=NULL
DECLARE @dbname VARCHAR(200)
DECLARE @sqlstr VARCHAR(8000)
CREATE TABLE #DBROLES (
DBName SYSNAME NOT NULL,
UserName SYSNAME NOT NULL,
db_owner VARCHAR(3) NOT NULL,
db_accessadmin VARCHAR(3) NOT NULL,
db_securityadmin VARCHAR(3) NOT NULL,
db_ddladmin VARCHAR(3) NOT NULL,
db_datareader VARCHAR(3) NOT NULL,
db_datawriter VARCHAR(3) NOT NULL,
db_denydatareader VARCHAR(3) NOT NULL,
db_denydatawriter VARCHAR(3) NOT NULL,
Cur_Date DATETIME NOT NULL DEFAULT GETDATE()
)
DECLARE DBName_Cursor CURSOR FOR
SELECT name FROM master.dbo.sysdatabases
WHERE name in ('master')
ORDER BY name
OPEN DBName_Cursor
FETCH NEXT FROM DBName_Cursor INTO @dbname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlstr = ' Insert into #DBROLES ( DBName, UserName, db_owner, db_accessadmin,
db_securityadmin, db_ddladmin, db_datareader, db_datawriter,
db_denydatareader, db_denydatawriter )
SELECT '
+''''+@dbName +''''+ ' as DBName ,UserName, '+CHAR(13)+ '
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
FROM (
SELECT b.name as USERName, c.name as RoleName
FROM '
+ @dbName+'.dbo.sysmembers a '+CHAR(13)+
' JOIN '+ @dbName+'.dbo.sysusers b '+CHAR(13)+
' on a.memberuid = b.uid JOIN '+@dbName +'.dbo.sysusers c
on a.groupuid = c.uid )s
GROUP BY USERName
ORDER BY UserName'
--Print @sqlstr
Execute (@sqlstr)
FETCH NEXT FROM DBName_Cursor INTO @dbname
END
CLOSE DBName_Cursor
DEALLOCATE DBName_Cursor
SELECT * FROM #DBRoles
WHERE ((@database is NULL) OR (DBName LIKE '%'+@database+'%')) AND
((@user is NULL) OR (UserName LIKE '%'+@user+'%')) AND
((@dbo is NULL) OR (db_owner = 'Yes')) AND
((@access is NULL) OR (db_accessadmin = 'Yes')) AND
((@security is NULL) OR (db_securityadmin = 'Yes')) AND
((@ddl is NULL) OR (db_ddladmin = 'Yes')) AND
((@datareader is NULL) OR (db_datareader = 'Yes')) AND
((@datawriter is NULL) OR (db_datawriter = 'Yes')) AND
((@denyread is NULL) OR (db_denydatareader = 'Yes')) AND
((@denywrite is NULL) OR (db_denydatawriter = 'Yes'))
ORDER BY UserName
DROP TABLE #DBROLES
No comments:
Post a Comment