Quantcast
Channel: SQL Archives - SQL Authority with Pinal Dave
Viewing all articles
Browse latest Browse all 594

SQL SERVER – Query to Get the List of Logins Having System Admin (sysadmin) Permission

$
0
0

Though the script sounded simple to me, but I found that there are lots of incorrect scripts available on the internet.  Here is the one of the script I found to find out the details of the system admin.

Please note that following script is not accurate and I do not encourage you to depend on it. You will find the correct script at the end of this article, please continue reading till the end of the blog post.

SELECT   name,type_desc,is_disabled
FROM     master.sys.server_principals 
WHERE    IS_SRVROLEMEMBER ('sysadmin',name) = 1
ORDER BY name

Above script looks very simple. When I ran on my machine I got below.

SQL SERVER - Query to Get the List of Logins Having System Admin (sysadmin) Permission who-sysadmin-01

I realized that some entries are missing. So, I went ahead and checked the properties of SysAdmin role and found below

SQL SERVER - Query to Get the List of Logins Having System Admin (sysadmin) Permission who-sysadmin-02

As we can see, I am not seeing all 6 members in the output. So, here is the query which I was able to write which would give accurate information.

SELECT 'Name' = sp.NAME
	,sp.is_disabled AS [Is_disabled]
FROM sys.server_role_members rm
	,sys.server_principals sp
WHERE rm.role_principal_id = SUSER_ID('Sysadmin')
	AND rm.member_principal_id = sp.principal_id

Here is the output, which is accurate.

SQL SERVER - Query to Get the List of Logins Having System Admin (sysadmin) Permission who-sysadmin-03

Do you have any similar interesting queries? Please share them with other readers via the comments section.

Reference: Pinal Dave (http://blog.sqlauthority.com)

First appeared on SQL SERVER – Query to Get the List of Logins Having System Admin (sysadmin) Permission


Viewing all articles
Browse latest Browse all 594

Trending Articles