SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

List all Usernames, Roles for all the databases.

By shivaram challa, 2008/10/02

Total article views: 5772 | Views in the last 30 days: 287

You can directly download the script from here: http://www.sqlservercentral.com/Forums/Attachment3502.aspx

Provided the link so that folks who are having Misc' characters issue can get this file and use it.


Description: This script generates a list of Usernames and the database Roles that the user belongs to in all the databases; with a couple of other useful information detailed below.

Other benefit: you can use this script to get the roles of ONE specific user in all the databases.

Directions of Use:

For All Users list: You can directly run this script in SQL Server Management studio

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 Explanation:

ServerName: Name of the server this script was executed on.
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

Tested: This script is tested on SQL Server 2005 standard edition with sp2 and the forum users were generous enough to test the script on SQL server 2000 and SQL Server 2008 and deemed the script working.

Change history:

08/26/2008 Shiva Challa - Removed the misc characters from the "Select of EXEC sp_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.groupuid" instead of "m.memberuid"
02/09/2009 Shiva Challa - Per Matt Karp's(forum member) suggestion, added Square brackets around dbname,
- changed dbname datatype to sysname instead of varchar(50)
- Added ServerName to the select

By shivaram challa, 2008/10/02

Total article views: 5772 | Views in the last 30 days: 287
Your response
 
 
Related Articles
FORUM

Database Default file Locations

Database Default Locations in Database Setting Under Server Properties

FORUM

Default Database

Finding Default Database

BLOG

Haidong Ji - Script to set default data and log file folder in SQL Server 2005

Hopefully your server has separate spindles for your database's data and log files. Here is the scri...

Tags
administration    
audit    
role    
security    
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com