Hi guys,
This is possibly along similar lines to one i raised on here a couple of months ago - but with a twist...
Am trying to produce a table for all users accross all databases on a server using the sp_MSforeachdb stored procedure.
Solution was to use the following code...
However as this lists all database roles as well, what i wanted to do was to create a table that held the username and then the roles that user had attached to it in a seperate column.
so now i have this...
What i am sturggling with is now running this latest script through the sp_MSforeachdb stored procedure.
What i have so far is...
but this is keeps throwing error saying
any ideas please?
Cheers.
This is possibly along similar lines to one i raised on here a couple of months ago - but with a twist...
Am trying to produce a table for all users accross all databases on a server using the sp_MSforeachdb stored procedure.
Solution was to use the following code...
Code:
CREATE TABLE ##Users
(DB VARCHAR(100),
UserName VARCHAR(100),
CreateDate DATETIME,
UpdateDate DATETIME)
EXEC master..sp_MSforeachdb
'USE [?]
INSERT INTO ##Users
SELECT DB_NAME() AS DB,
[name], createdate,
updatedate
FROM sysusers'
SELECT * FROM ##Users
DROP TABLE ##Users
However as this lists all database roles as well, what i wanted to do was to create a table that held the username and then the roles that user had attached to it in a seperate column.
so now i have this...
Code:
Create Table #Temp_Users
(
Name varchar(128),
CreateDate datetime,
LastModifiedDate datetime,
LoginType varchar(50),
Roles varchar(1024)
)
Create Table #Temp_Roles
(
Name varchar(128),
Role varchar(128)
)
insert into #Temp_Users
select Name, [Create Date] = CreateDate, [Last Modified Date] = UpdateDate,
LoginType = case
when IsNTName = 1 then 'Windows Account'
when IsNTGroup = 1 then 'Windows Group'
when isSqlUser = 1 then 'SQL Server User'
when isAliased =1 then 'Aliased'
when isSQLRole = 1 then 'SQL Role'
when isAppRole = 1 then 'Application Role'
else 'Unknown'
end,
Roles = ''
from sysusers
where SID is not null
order by Name
insert into #Temp_Roles
select MemberName = u.name, DbRole = g.name
from sysusers u, sysusers g, sysmembers m
where g.uid = m.groupuid
and g.issqlrole = 1
and u.uid = m.memberuid
order by 1, 2
Declare @Name varchar(128)
Declare @Roles varchar(1024)
Declare @Role varchar(128)
DECLARE UserCursor CURSOR for
SELECT name from #Temp_Users
OPEN UserCursor
FETCH NEXT FROM UserCursor into @Name
WHILE @@FETCH_STATUS = 0
BEGIN
set @Roles = ''
print @Name
DECLARE RoleCursor CURSOR for
SELECT Role from #Temp_Roles where Name = @Name
OPEN RoleCursor
FETCH NEXT FROM RoleCursor into @Role
WHILE @@FETCH_STATUS = 0
BEGIN
if (@Roles > '')
set @Roles = @Roles + ', '+@Role
else
set @Roles = @Role
FETCH NEXT FROM RoleCursor into @Role
end
Close RoleCursor
DEALLOCATE RoleCursor
Update #Temp_Users set Roles = @Roles where Name = @Name
FETCH NEXT FROM UserCursor into @Name
END
CLOSE UserCursor
DEALLOCATE UserCursor
select * from #Temp_Users
drop table #Temp_Users
drop table #Temp_Roles
What i am sturggling with is now running this latest script through the sp_MSforeachdb stored procedure.
What i have so far is...
Code:
Create Table ##Temp_Users
(DB varchar(100),
Name varchar(128),
CreateDate datetime,
LastModifiedDate datetime,
LoginType varchar(50),
Roles varchar(1024)
)
Create Table ##Temp_Roles
(
Name varchar(128),
Role varchar(128)
)
Declare @@Name varchar(128)
Declare @@Roles varchar(1024)
Declare @@Role varchar(128)
EXEC master.sys.sp_MSforeachdb
'USE [?]
insert into ##Temp_Users
select DB_Name(),Name, [Create Date] = CreateDate, [Last Modified Date] = UpdateDate,
LoginType = case
when IsNTName = 1 then ''Windows Account''
when IsNTGroup = 1 then ''Windows Group''
when isSqlUser = 1 then ''SQL Server User''
when isAliased =1 then ''Aliased''
when isSQLRole = 1 then ''SQL Role''
when isAppRole = 1 then ''Application Role''
else ''Unknown''
end,
Roles = ''
from sysusers
where SID is not null
order by Name
insert into ##Temp_Roles
select MemberName = u.name, DbRole = g.name
from sysusers u, sysusers g, sysmembers m
where g.uid = m.groupuid
and g.issqlrole = 1
and u.uid = m.memberuid
order by 1, 2
DECLARE UserCursor CURSOR for
SELECT name from ##Temp_Users
OPEN UserCursor
FETCH NEXT FROM UserCursor into @@Name
WHILE @@FETCH_STATUS = 0
BEGIN
set @@Roles = ''
print @@Name
DECLARE RoleCursor CURSOR for
SELECT Role from #Temp_Roles where Name = @@Name
OPEN RoleCursor
FETCH NEXT FROM RoleCursor into @@Role
WHILE @@FETCH_STATUS = 0
BEGIN
if (@@Roles > '')
set @@Roles = @@Roles + ', '+@@Role
else
set @@Roles = @@Role
FETCH NEXT FROM RoleCursor into @@Role
end
Close RoleCursor
DEALLOCATE RoleCursor
Update ##Temp_Users set Roles = @@Roles where Name = @@Name
FETCH NEXT FROM UserCursor into @@Name
END
CLOSE UserCursor
DEALLOCATE UserCursor'
select * from ##Temp_Users
drop table ##Temp_Users
drop table ##Temp_Roles
but this is keeps throwing error saying
Code:
Msg 911, Level 16, State 1, Line 1
Could not locate entry in sysdatabases for database '?'. No entry found with that name. Make sure that the name is entered correctly.
any ideas please?
Cheers.