Skip to content

Instantly share code, notes, and snippets.

@jrdmb
Last active August 29, 2015 14:18
Show Gist options
  • Save jrdmb/5d7bdce2757344b22e13 to your computer and use it in GitHub Desktop.
Save jrdmb/5d7bdce2757344b22e13 to your computer and use it in GitHub Desktop.
Sql Server database role members
-- Database Role Members
SELECT Users.name AS UserName, Roles.name AS RoleName,
'EXEC sp_droprolemember '+QUOTENAME(Roles.name,'''')+','+QUOTENAME(Users.name,'''')+';' AS Drop_Script,
'EXEC sp_addrolemember '+QUOTENAME(Roles.name,'''')+','+QUOTENAME(Users.name,'''')+';' AS Add_Script
FROM sys.database_role_members RoleMembers
JOIN sys.database_principals Users
ON RoleMembers.member_principal_id = Users.principal_id
JOIN sys.database_principals Roles
ON RoleMembers.role_principal_id = Roles.principal_id
--WHERE Users.name LIKE '%MyUserName%'
--WHERE Roles.name LIKE '%DBRoleName%'
#tsql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment