I would like to write a query on a sql 2008 that will report all the users that have access to a specific database, or objects within the database such as tables, views, and stored procedures, either directly or due to roles, etc. This report would be used for security auditing purposes. Not sure if anyone has a query that will fit my needs completely, but hopefully something that will give me a good start. Either sql 2008, 2005 or 2000 will do, I can probably convert as needed.
相关问题
- sql execution latency when assign to a variable
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
- SQL to Parse a Key-Value String
相关文章
- Entity Framework 4.3.1 failing to create (/open) a
- Code for inserting data into SQL Server database u
- SQL Server 2008 Change Data Capture, who made the
- Delete Every Alternate Row in SQL
- Linux based PHP install connecting to MsSQL Server
- SQL Azure Reset autoincrement
- How do we alias a Sql Server instance name used in
- Is recursion good in SQL Server?
Here is a complete version of Jeremy's Aug 2011 query with the changes suggested by Brad (Oct 2011) and iw.kuchin (May 2012) incorporated:
[ObjectType]
and[ObjectName]
for schemas.[ObjectType]
it's better to useobj.type_desc
only forOBJECT_OR_COLUMN
permission class. For all other cases useperm.[class_desc]
.IMPERSONATE
permissions.sys.login_token
withsys.server_principals
as it will show also SQL Logins, not only Windows ones.sys
and INFORMATION_SCHEMA.Hopefully this saves someone else an hour or two of their lives.
:)
The other answers that I have seen miss some permissions that are possible in the database. The first query in the code below will get the database level permission for everything that is not a system object. It generates the appropriate GRANT statements as well. The second query gets all the role meberships.
This has to be run for each database, but is too long to use with sp_MSforeachdb. If you want to do that you'd have to add it to the master database as a system stored procedure.
To cover all possibilities you'd also have to have a script that checks the server level permissions.
From SQL Server 2005 on, you can use system views for that. For example, this query lists all users in a database, with their rights:
Be aware that a user can have rights through a role as well. For example, the
db_data_reader
role grantsselect
rights on most objects.Great thanks for awesome audit scripts.
I highly recommend for audit user use awesome Kenneth Fisher (b | t) stored procedures: