This is how I would match two:
DECLARE @rolename sysname = 'role_name'; DECLARE @cmd AS NVARCHAR(MAX) = N''; SELECT @cmd = @cmd + ' ALTER ROLE ' + QUOTENAME(@rolename) + ' DROP MEMBER ' + QUOTENAME(members.[name]) + ';' FROM sys.database_role_members AS rolemembers JOIN sys.database_principals AS roles ON roles.[principal_id] = rolemembers.[role_principal_id] JOIN sys.database_principals AS members ON members.[principal_id] = rolemembers.[member_principal_id] WHERE roles.[name] =@rolename EXEC(@cmd);
This creates a line with your ALTER ROLE for each line (user) in your request, combines them all together into one large line with all these commands, and then dynamically executes them.
source share