/** Script: list all Usernames, Roles for all the databases. Author: Shiva Challa (http://challa.

info) and the database Roles that the user belongs to in all the databases. Also, you can use this script to get the roles of one user in all the databases. Directions of Use: For All Users list: You can directly run this script in SQL Server Management st udio For a specific user: ????????1. Find this code and u.name like ''tester'' ????????2. Uncomment the code ????????3. Replace the Name ''tester'' with the username you want to search on. Resultset: ????????DBName: Database name that the user exists in. ????????Name: user name. ????????GroupName: Group/Database Role that the user is a part of. ????????LoginName: Actual login name, if this is null, Name is used to connect. ????????default_database_name ????????default_schema_name???? ????????principal_id ????????sid Change History: 08/26/2008 Shiva Challa - Removed the misc characters from the "Select of EXEC s p_MSForEachdb" statement. 09/02/2008 Cathy Greenselder - Convert to SQL2000 - (default_database_name not in SQL2K) - (default_schema_name not in SQL2K) - (principal_id not in SQL2K) - (uid is in SQL2K 10/08/2008 Shiva Challa - Added Cathy's script to the original script with an IF logic to make it work for both SQL 2000 and SQL 2005. - Added code to use SysObjects in 2000 instead of sys.objects 10/09/2008 Cathy Greenselder - for 2000, switched the SysUsers join to use "m.gr oupuid" instead of "m.memberuid" 02/09/2009 Shiva Challa????- Per Matt Karp's(forum member) suggestion, added Squ are brackets around dbname, - changed dbname datatype to sysname - Added ServerName as well. **/ USE MASTER GO BEGIN IF LEFT(CAST(Serverproperty('ProductVersion') AS VARCHAR(1)), ????????????1) = '9' ????IF EXISTS (SELECT TOP 1 * ???????????? FROM Tempdb.sys.objects (nolock) ???????????? WHERE name LIKE '#TUser%') ????DROP TABLE #TUser ????ELSE ????IF LEFT(CAST(Serverproperty('ProductVersion') AS VARCHAR(1)), ????????????1) = '8' ????IF EXISTS (SELECT TOP 1 * ???????????? FROM Tempdb.dbo.sysobjects (nolock) ???????????? WHERE name LIKE '#TUser%')

???? DBName SYSNAME. ???????? u. ???? default_schema_name VARCHAR(256) NULL.default_schema_name.name ???????? END AS GroupName. ???? LoginName SYSNAME NULL.memberuid = u. ???? default_database_name SYSNAME NULL. ???? GroupName SYSNAME NULL.default_database_name.????DROP TABLE #TUser ???? ????CREATE TABLE #tuser ( ???? ServerName????????????varchar(256).dbo. .name like ''tester''*/ ???????? ORDER BY u. ???????? NULL as default_Schema_name. ???????? u. ???????? u.sysLogins l ???????? ON u.name AS LoginName.sid ???????? WHERE u.principal_id IS NULL) THEN ''public'' ???????? ELSE r. ???????? l. ???????? u.sysUsers u ???????? LEFT JOIN ([?].principal_id. ???????? l. ???????? u.isntgroup = 1 ???????? /*and u. ???????? CASE ???????? WHEN (r. ???????? ''?'' as DBName.sysUsers r ???????? ON m.sid ???????? FROM [?].uid. ???? Principal_id INT.name ???????? ' ????ELSE ????IF LEFT(CAST(Serverproperty('ProductVersion') AS VARCHAR(1)). ???????? CASE ???????? WHEN (r. ???????? u. ???????? NULL AS Default_db_Name. ????????????1) = '9' ????INSERT INTO #TUser ????EXEC sp_MSForEachdb ????????' ???????? SELECT ???????? @@SERVERNAME. ???? [Name] SYSNAME.uid) ???????? ON m.groupuid = r.name LoginName. ???? sid VARBINARY(85)) ???? ????IF LEFT(CAST(Serverproperty('ProductVersion') AS VARCHAR(1)). ???????? l.isntname = 1 OR u.name ???????? END GroupName.dbo. ????????????1) = '8' ????INSERT INTO #TUser ????EXEC sp_MSForEachdb ????????' ???????? SELECT ???????? @@SERVERNAME.name.uid ???????? LEFT JOIN dbo.sid = l.dbo. ???????? ''?''.sysMembers m ???????? JOIN [?].islogin = 1 OR u.name As UserName.uid IS NULL) THEN ''public'' ???????? ELSE r.

server_principals l ???????? ON u. ???????????? [name].sys.sys.database_principals r ???????? ON m.principal_id ???????? LEFT JOIN [?].name like ''tester''*/ ???????? order by u.sid = l.sid ???????? FROM [?].role_principal_id = r.name ???????? ' ???? ????SELECT * ????FROM #TUser ????ORDER BY DBName.sys.database_principals u ???????? LEFT JOIN ([?].principal_id) ???????? ON m. ???????????? GroupName ???? ????DROP TABLE #TUser END .member_principal_id = u.???????? u.sid ???????? WHERE u.database_role_members m ???????? JOIN [?].TYPE <> ''R'' ???????? /*and u.sys.

Sign up to vote on this title
UsefulNot useful