SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

WJHamel
Aged Yak Warrior

USA
645 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

3662 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: http://msdn.microsoft.com/en-us/library/ms187328.aspx
Go to Top of Page

robvolk
Most Valuable Yak

USA
15676 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  
 New 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.03 seconds. Powered By: Snitz Forums 2000