Beschreibung der Funktion

Das folgende Skript ermittelt automatisch alle Serverrollen (sowohl die neun festen als auch benutzerdefinierte Rollen) 

 

SELECT
    @@SERVERNAME AS ServerName,
    m.name AS LoginName,
    STUFF((
        SELECT ',' + r2.name
        FROM sys.server_role_members rm2
        INNER JOIN sys.server_principals r2
            ON rm2.role_principal_id = r2.principal_id
        WHERE rm2.member_principal_id = m.principal_id
          AND r2.type = 'R'
        ORDER BY r2.name
        FOR XML PATH('')
    ), 1, 1, '') AS Roles
FROM sys.server_principals m
WHERE m.type IN ('S', 'U', 'G')   -- SQL-Login, Windows-User, Windows-Gruppe
  AND m.principal_id > 4          -- System-Logins ausblenden (sa, ##MS_...)
  AND EXISTS (
      SELECT 1 FROM sys.server_role_members rm
      WHERE rm.member_principal_id = m.principal_id
  )
ORDER BY m.name;

 

Ausgabe-Beispiel

 
 
No ServerName LoginName RoleName IsMember
1 SRV01 app_user bulkadmin X
2 SRV01 app_user public X
3 SRV01 sa public X
4 SRV01 sa sysadmin X
5 SRV01 DOMAIN\joe securityadmin X

 

Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Advertisement
If you accept, the ads on the page will be adapted to your preferences.
Google Ad
Save