Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Security Queries: Database-level

By Greg Drake,

This script contains various queries I have written on the subject of security as it applies to a specific database. It's arranged so I can either (A) run one query I need alone or (B) run the whole thing as a batch to dump out everything at once.

Option B might not seem useful at first (information overload!), but consider that if your output in SSMS is set to "Results to Text", this will allow you to quickly copy off a single text file that contains everything you might want to know about that database's security at a point in time. Furthermore, the queries are carefully structured and ordered to be consistent. This makes two of these text files easy to compare with a standard compare/diff/merge application (e.g. WinMerge, Beyond Compare).

Included queries:

  • Database roles
  • Database users
  • Database role membership
  • Database role and user permissions
  • Database-level SQL/Assembly module EXECUTE AS
  • Schema ownership
  • Individual ownership of database objects if not owned by schema
  • Database users linked to server login
  • Database users that were linked to server login, but are not anymore

The last query is a re-write of the 'report' mode of sp_change_users_login.

Please also see related script Security Queries: Server-level.

Total article views: 3110 | Views in the last 30 days: 876
Related Articles

how to back up and restore security user login [database logins] from sql 2000 to sql 2005 x64

back up and restore security user login [database logins]


Secure Login

Secure Mechanism For Customer Login page


Separate Login for a query in a stored procedure

separate login for query


Security Queries: Server-level

A set of queries which attempt to gather as much security-related information on a server instance a...


SQL Server Security Extract (Reverse Engineer) for ALL Databases

These scripts will extract security for Windows Logins, Database Users, Roles, Objects and SQL Login...


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones