我的主管要求我创建一个ssrs报告(使用sql server 2008),该报告将显示用户有权访问的所有数据库名称,并确保用户仍有活动帐户。我可以查询来自不同数据库的用户名,但我似乎无法列出用户有权访问的数据库名称。
有没有办法实现这个事件,尽管有些数据库位于不同的链接服务器中。
提前感谢大家,非常感谢任何指导我正确方向的帮助。
答案 0 :(得分:0)
DECLARE @DBuser_table TABLE (DBName VARCHAR(200),
UserName VARCHAR(250),
LoginType VARCHAR(500),
AssociatedRole VARCHAR(200))
DECLARE @UserName VARCHAR(250)
SET @UserName = 'dbo' --change this to desired username
SET @DBuser_sql='SELECT ''?'' AS DBName,a.name AS Name,a.type_desc AS LoginType,USER_NAME(b.role_principal_id) AS AssociatedRole FROM ?.sys.database_principals a
LEFT OUTER JOIN ?.sys.database_role_members b ON a.principal_id=b.member_principal_id
WHERE a.sid NOT IN (0x01,0x00) AND a.sid IS NOT NULL AND a.type NOT IN (''C'') AND a.is_fixed_role <> 1 AND a.name NOT LIKE ''##%'' AND ''?'' NOT IN (''master'',''msdb'',''model'',''tempdb'') ORDER BY Name'
INSERT @DBuser_table
EXEC sp_MSforeachdb @command1=@DBuser_sql
SELECT *
FROM @DBuser_table
WHERE username = @UserName
ORDER BY DBName