July 11, 2012 at 2:22 pm
How can i find list databases that has user "Domain\JSmith" ?
July 11, 2012 at 2:49 pm
Query sys.database_principals in each database for that user.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
July 11, 2012 at 2:59 pm
and a code example of what Jason was talking about:
sp_msForEachDB 'select ''?'' AS DatabaseName,
name AS UserName
FROM [?].sys.database_principals
WHERE name = ''Domain\JSmith'' '
Lowell
July 11, 2012 at 4:54 pm
i have more than 400 dbs so i cant use spmsforeachdb as it wonts display everything in the result, is there a way i can just get the list of only those db that has this user?
July 11, 2012 at 5:05 pm
Tara-1044200 (7/11/2012)
i have more than 400 dbs so i cant use spmsforeachdb as it wonts display everything in the result, is there a way i can just get the list of only those db that has this user?
Sure you can use msforeachdb, it just takes some tweaking.
CREATE TABLE #UserExists (DatabaseName VARCHAR(128), UserName VARCHAR(128));
GO
EXECUTE sp_msForEachDB 'Insert Into #UserExists (DatabaseName, UserName)
select ''?'' AS DatabaseName,
name AS UserName
FROM [?].sys.database_principals
WHERE name = ''Domain\JSmith'' ';
SELECT DatabaseName, UserName
FROM #UserExists;
DROP TABLE #UserExists;
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply