Help!
I'm working on a stored procedure to pull back all users by database. Unfortunately, I can't find a good consistent way to connect the users to the databases. I've tried going through master.dbo.syscacheobjects and then I tried going through master.dbo.sysprocesses. The problem with going that route is that it only pulls users currently connected or that have processes running.
Finally, I came up with a cursor that is supposed to cycle through each database and pull up the users in each db's SysUsers table, but it pulls up ALL users, regardless of whether they have access to the current database, and appends in the current database name.
I'm at my wits end. Any help would be greatly appreciated. Here's the code:
Any thoughts or ideas on what I'm doing wrong?
Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
I'm working on a stored procedure to pull back all users by database. Unfortunately, I can't find a good consistent way to connect the users to the databases. I've tried going through master.dbo.syscacheobjects and then I tried going through master.dbo.sysprocesses. The problem with going that route is that it only pulls users currently connected or that have processes running.
Finally, I came up with a cursor that is supposed to cycle through each database and pull up the users in each db's SysUsers table, but it pulls up ALL users, regardless of whether they have access to the current database, and appends in the current database name.
I'm at my wits end. Any help would be greatly appreciated. Here's the code:
Code:
Create table #DBPermits(LoginName varchar(75), DatabaseName varchar(75),
DatabaseRole varchar(75), objectName varchar(300), HasDBAccess bit,
Action varchar(30),
AccessType varchar(15), CreateDate smalldatetime,
UpdateDate smalldatetime, WindowsGroup char(3), WindowsUser char(3),
SQLUser char(3), SQLRole char(3), AppRole char(3))
Declare @DBName varchar(75)
Declare DBCur CURSOR GLOBAL for
Select Name from master.dbo.sysdatabases
Order by Name
Open DBCur
FETCH NEXT from DBCur into @DBName
WHILE @@Fetch_Status = 0
BEGIN
Insert into #DBPermits(LoginName, DataBaseName, DatabaseRole, objectName,
HasDBAccess, Action, AccessType, CreateDate, UpdateDate, WindowsGroup,
WindowsUser, SQLUser, SQLRole, AppRole)
(select Distinct sysusers.name as LoginName, @DBName,
Case when B.Name IS NULL then
'No Database Roles'
else
B.Name
end DatabaseRole,
sysobjects.name as ObjectName, sysusers.hasdbaccess,
case when action = 193 then
'SELECT'
when action = 195 then
'INSERT'
when action = 196 then
'DELETE'
when action = 197 then
'UPDATE'
when action = 224 then
'EXECUTE'
when action = 178 then
'CREATE FUNCTION'
when action = 198 then
'CREATE TABLE'
when action = 203 then
'CREATE DATABASE'
when action = 207 then
'CREATE VIEW'
when action = 222 then
'CREATE PROCEDURE'
when action = 228 then
'BACKUP DATABASE'
when action = 233 then
'CREATE DEFAULT'
when action = 235 then
'BACKUP LOG'
when action = 236 then
'CREATE RULE'
when action = 26 then
'REFERENCES / DRI'
else
'SOME OTHER RIGHT'
end 'ACTION',
case when protecttype = 204 then
'GRANT_W_GRANT'
when protecttype = 205 then
'GRANT'
when protecttype = 206 then
'DENY'
end 'AccessType', sysusers.CreateDate, sysusers.UpdateDate,
Case when isntgroup = 1 then
'YES'
when isntgroup = 0 then
'NO'
end 'WindowsGroup',
Case when isntuser = 1 then
'YES'
when isntuser = 0 then
'NO'
end 'WindowsUser',
Case when issqluser = 1 then
'YES'
when issqluser = 0 then
'NO'
end 'SQLUser',
Case when issqlrole = 1 then
'YES'
when issqlrole = 0 then
'NO'
end 'SQLRole',
Case when isapprole = 1 then
'YES'
when isapprole = 0 then
'NO'
end 'AppRole'
from sysprotects
join sysobjects on sysprotects.id = sysobjects.id
join sysusers on sysprotects.uid = sysusers.uid
left outer join (Select sysusers.UID, sysusers.Name,
sysmembers.MemberUid
from sysusers
join sysmembers
on sysusers.uid = sysmembers.groupuid
where sysusers.issqlrole = 1) B
on sysusers.Uid = b.memberUid)
-- Apacs database users & permissions
FETCH NEXT from DBCur into @DBName
END
Close DBCur
Deallocate DBCur
Select * from #DBPermits
Order by DatabaseName, LoginName
Drop table #DBPermits
Any thoughts or ideas on what I'm doing wrong?
Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"