Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select system dbs
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Aged Yak Warrior

651 Posts

Posted - 04/18/2013 :  10:59:28  Show Profile  Reply with Quote
I need a select statement which show me a list of the system db's, the users associated with each db, the roles associated with each db and the roles associated with each db.

i know this is readily accessible from the object explorer, but i need a query resultset that shows me the same.

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 04/18/2013 :  11:31:30  Show Profile  Reply with Quote
There are only 4 system dbs (if you don't count resource db), so list them explicitly using an IN clause.

To list users and roles, use sys.database_principals system view. The one-character type column indicates which type of principal it is. What each of those codes mean is listed here:
Go to Top of Page

Most Valuable Yak

15732 Posts

Posted - 04/18/2013 :  11:43:25  Show Profile  Visit robvolk's Homepage  Reply with Quote
SELECT name FROM sys.databases WHERE database_id<=5

EXEC sp_msforeachdb 'USE [?];SELECT ''?'' db_name, name from sys.database_principals where type<>''R'' '
EXEC sp_msforeachdb 'USE [?];SELECT ''?'' db_name, name from sys.database_principals where type=''R'' '
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000