Sql-server – Query to extract all SQL database roles with INSERT and/or UPDATE privileges

sql serversql-server-2008sql-server-2008-r2

I've taken over the support for a SQL 2008 database and there are a multitude of custom roles (25+) associated within. I'm trying to figure out which of the custom roles have the ability to perform privileged INSERT and/or UPDATE actions within the database.

Can anyone help me write a query to run against the database which would return a listing of all roles with INSERT and UPDATE privileges?

Any help would be much appreciated, Thank you!

Best Answer

Here is a starting point. The first query will give you a list of roles with either INSERT or UPDATE permission. From there you will need to look at the permissions being at the DATABASE, SCHEMA, or OBJECT level and decide if they cover the objects you are interested in. Check the state_desc column to make sure it is GRANT (it probably will be) as DENYs are obviously the opposite of what you want.

The second query will tell you any role that is a member of the db_datawriter role. These roles will also have INSERT and UPDATE permissions.

SELECT dprin.name, dperm.* 
FROM sys.database_permissions dperm
JOIN sys.database_principals dprin
    ON dperm.grantor_principal_id = dprin.principal_id
WHERE permission_name IN ('INSERT','UPDATE')
  AND dprin.type = 'R'

SELECT drole.name as role_name, dprin.name as role_member
FROM sys.database_role_members rmem
JOIN sys.database_principals drole
    ON drole.principal_id = rmem.role_principal_id 
JOIN sys.database_principals dprin
    ON dprin.principal_id = rmem.member_principal_id 
WHERE drole.name = 'db_datawriter'
  AND dprin.type = 'R'