Skip to content

Instantly share code, notes, and snippets.

@suatatan
Created January 29, 2025 07:42
Show Gist options
  • Select an option

  • Save suatatan/82543d18cd82fdb43a1feec7268704ad to your computer and use it in GitHub Desktop.

Select an option

Save suatatan/82543d18cd82fdb43a1feec7268704ad to your computer and use it in GitHub Desktop.
Adding User to DB and verifying
-- CREATE USER SuatATAN with PASSWORD ='PASSWORD'
-- ALTER ROLE db_datareader ADD MEMBER SuatATAN
SELECT
dp.name AS UserName,
dp.type_desc AS UserType,
STRING_AGG(rp.name, ', ') AS AssignedRoles
FROM sys.database_principals dp
LEFT JOIN sys.database_role_members drm ON dp.principal_id = drm.member_principal_id
LEFT JOIN sys.database_principals rp ON drm.role_principal_id = rp.principal_id
WHERE dp.type IN ('S', 'U', 'G') -- S = SQL User, U = Windows User, G = Group
GROUP BY dp.name, dp.type_desc
ORDER BY dp.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment