我目前在 SQL Server 2005 中使用以下代码来获取有关孤立用户 SID 的数据,这很好用,但我确实需要获取有关每个孤立用户 SID 的角色成员身份的信息,这可能吗?如果可以,我该如何编写查询为了这?
DECLARE cur CURSOR FAST_FORWARD FOR
SELECT name FROM sys.databases
WHERE database_id > 4
OPEN cur
DECLARE @SQL NVARCHAR(MAX), @DBName SYSNAME
DECLARE @Results TABLE (DBName SYSNAME, UserName SYSNAME, UserSID VARBINARY(MAX))
FETCH NEXT FROM cur into @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'USE ' + @DBName + ';SELECT ''' + @DBName + ''' AS DBName,
UserName = name, UserSID = sid from sysusers
WHERE issqluser = 1 AND
(sid IS NOT NULL AND sid <> 0x0) AND
(LEN(sid) <= 16) AND SUSER_SNAME(sid) IS NULL'
INSERT INTO @Results
EXEC(@SQL)
FETCH NEXT FROM cur into @DBName
END
CLOSE cur
DEALLOCATE cur
SELECT * FROM @Results
谢谢!
感谢您的回复,我已经根据迄今为止提供的信息创建了以下查询,这看起来像它提供了我需要的东西?
select u.uid, u.name, u.sid, rm.role_principal_id as 'Role ID', dp1.name as 'Role'
from sys.sysusers u
left join sys.syslogins l on UPPER(u.sid) = UPPER(l.sid)
inner join sys.database_role_members rm on rm.member_principal_id = u.uid
left join sys.database_principals dp on dp.principal_id = rm.member_principal_id
left join sys.database_principals dp1 on dp1.principal_id = rm.role_principal_id
where u.uid > 4 and u.issqlrole = 0
and issqluser = 1
and l.name is null
order by u.name