MSSQL: List database membership roles of each user in several databases

Koczka Bence

I have several databases, which all have users under the database/security/users folder (shown on the left side of the picture:

image displaying the membership roles I am looking for

I need a query, which could list each user's role memberships (shown on the right side of the picture above, current user, called User2 has none). These are the roles I am looking for to list for each users.

Here is something similar I used before, for a different purpose (listing the server roles of logins under the /Security/Logins folder, instead of membership roles of users under the DatabaseName/Security/Users folder):

SELECT
    spU.name
    ,MAX(CASE WHEN srm.role_principal_id = 3 THEN 1 END) AS sysadmin
    ,MAX(CASE WHEN srm.role_principal_id = 4 THEN 1 END) AS securityadmin
    ,MAX(CASE WHEN srm.role_principal_id = 5 THEN 1 END) AS serveradmin
    ,MAX(CASE WHEN srm.role_principal_id = 6 THEN 1 END) AS setupadmin
    ,MAX(CASE WHEN srm.role_principal_id = 7 THEN 1 END) AS processadmin
    ,MAX(CASE WHEN srm.role_principal_id = 8 THEN 1 END) AS diskadmin
    ,MAX(CASE WHEN srm.role_principal_id = 9 THEN 1 END) AS dbcreator
    ,MAX(CASE WHEN srm.role_principal_id = 10 THEN 1 END) AS bulkadmin
FROM
    sys.server_principals AS spR
JOIN
    sys.server_role_members AS srm
ON
    spR.principal_id = srm.role_principal_id
JOIN
    sys.server_principals AS spU
ON
    srm.member_principal_id = spU.principal_id
WHERE
    spR.[type] = 'R'
    and spU.name not like '##MS%'
    and spU.name not like 'NT%'
GROUP BY
    spU.name

These are not the roles I am looking for, this is just here to make it more easier to understand in what format I would like my output to be. Anyway, the output of this query looks like this:

enter image description here

A similar output for my current task (to list the membership roles of each users under the database/security/users/ folder) would be great, but any other ways to list the membership roles is welcome.

With the membership role listing query done for each users in the current database, I want to make it work on several databases, which will be done with the help of sp_MSforeachdb. I will edit this post when I'll have managed to work it out.

Koczka Bence

This id the code that finally worked, if someone would need it:

EXEC sp_MSforeachdb '
IF ''?'' NOT IN (''master'',''msdb'',''tempdb'', ''model'')
BEGIN
USE [?]
SELECT
    spU.name
    ,MAX(CASE WHEN srm.role_principal_id = 16384 THEN 1 END) AS db_owner
    ,MAX(CASE WHEN srm.role_principal_id = 16385 THEN 1 END) AS db_accessadmin
    ,MAX(CASE WHEN srm.role_principal_id = 16386 THEN 1 END) AS db_securityadmin
    ,MAX(CASE WHEN srm.role_principal_id = 16387 THEN 1 END) AS db_dlladmin
    ,MAX(CASE WHEN srm.role_principal_id = 16389 THEN 1 END) AS db_backupoperator
    ,MAX(CASE WHEN srm.role_principal_id = 16390 THEN 1 END) AS db_datareader
    ,MAX(CASE WHEN srm.role_principal_id = 16391 THEN 1 END) AS db_datawriter
    ,MAX(CASE WHEN srm.role_principal_id = 16392 THEN 1 END) AS db_denydatareader
    ,MAX(CASE WHEN srm.role_principal_id = 16393 THEN 1 END) AS db_denydatawriter
FROM
    [?].sys.database_principals AS spR
JOIN
    [?].sys.database_role_members AS srm
ON
    spR.principal_id = srm.role_principal_id
JOIN
    [?].sys.database_principals AS spU
ON
    srm.member_principal_id = spU.principal_id
GROUP BY
    spU.name
 
END'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I check asp membership database roles for a user in JavaScript?

MySQL : One database for all users or multiple databases for each user

Load list of roles for each user in MVC5 razor

One membership for each user for each club

MongoDB Roles - user access multiple databases

Powershell to read a list Usernames to show the AD group membership of each user and to output this to CSV file

ASP.net 4.5 Membership and Roles add user to role

Get each the "User" of child roles

Ways to put user roles in a database

How to create an edge list for each user mentioned in a tweet when there are observations containing several user mentioned

Running a subscription each time user changes roles

Best practice for designing user/roles/people in database

User with multiple roles and multiple teams database design

Sitefinity Retrieve All User and Roles from Database

ASP.NET Membership provider unable to return user roles in LINQ query

ASP.NET Roles and Membership

How to list all the roles existing in informix database?

Discord.net get the list of roles for a user

ASP.Net MVC List of User and their Roles

PostgreSQL: List databases user has privilege to connect

PostgreSQL: Query user from table along with list of user roles

Wordpress multisite - How to retrieve all user roles in each site in the network

User control: For each object in the database

Different mlocate database for each user?

save table data for each user with nodejs(can't use databases)

How to find which Database Roles are associated with a given User?

Symfony 4 Doctrine, How to load user Roles from the Database

How to use authorisation with user roles with my own database

ADO.NET how to identify a user's roles in the database?