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?
The GetPermissions Stored Procedure above is good however it uses Sp_msforeachdb which means that it will break if your SQL Instance has any databases names that include spaces or dashes and other non-best-practices characters. I have created a version that avoids the use of Sp_msforeachdb and also includes two columns that indicate 1 - if the Login is a sysadmin login (IsSysAdminLogin) and 2 - if the login is an orphan user (IsEmptyRow).
Can't comment on accepted answer so I'll add some comments here:
sys.objects
table contains only schema-scoped objects. So to get info about "higher level" objects (i.e. schemas in our case) you need to usesys.schemas
table.[ObjectType]
it's better to useobj.type_desc
only forOBJECT_OR_COLUMN
permission class. For all other cases useperm.[class_desc]
IMPERSONATE
. To get info about impersonations one shouldLEFT JOIN
withsys.database_principals
onperm.major_id = imp.principal_id
sys.login_token
withsys.server_principals
as it will show also SQL Logins, not only Windows ones'G'
to allowed principal types to allow Windows groupssys
andINFORMATION_SCHEMA
from resulting table, as these users are used only for serviceI'll post first piece of script with all proposed fixes, other parts should be changed as well:
Awesome script Jeremy and contributors! Thanks!
I have a s-ton of users, so running this for all users was a nightmare. I couldn't add comments, so I am posting the whole script with the changes. I added a variable + where clause so I can search for anything matching up to 5 characters in the user name (or all users when left blank). Nothing special, but I thought it would be helpful in some use cases.
This is my first crack at a query, based on Andomar's suggestions. This query is intended to provide a list of permissions that a user has either applied directly to the user account, or through roles that the user has.
Here is my version, adapted from others. I spent 30 minutes just now trying to remember how I came up with this, and @Jeremy 's answer seems to be the core inspiration. I didn't want to update Jeremy's answer, just in case I introduced bugs, so I am posting my version of it here.
Unfortunately I couldn't comment on the Sean Rose post due to insufficient reputation, however I had to amend the "public" role portion of the script as it didn't show SCHEMA-scoped permissions due to the (INNER) JOIN against sys.objects. After that was changed to a LEFT JOIN I further had to amend the WHERE-clause logic to omit system objects. My amended query for the public perms is below.