Skip to content

Instantly share code, notes, and snippets.

@Otterpohl
Created May 13, 2022 14:19
Show Gist options
  • Save Otterpohl/75ad458d13419a550b2158acdd42c1c1 to your computer and use it in GitHub Desktop.
Save Otterpohl/75ad458d13419a550b2158acdd42c1c1 to your computer and use it in GitHub Desktop.
Gets users who have been orphaned
SELECT dp.name AS [user_name],
dp.type_desc AS [user_type],
ISNULL(sp.name, 'Orphaned!') AS [login_name],
sp.type_desc AS [login_type]
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp
ON (dp.sid = sp.sid)
WHERE dp.type IN ( 'S', 'U', 'G' )
AND dp.principal_id > 4
ORDER BY sp.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment