You can store the output of the stored procedure sp_helpuser
in a table variable, and query on that:
declare @groups table (
UserName varchar(max),
GroupName varchar(max),
LoginName varchar(max),
RefDBName varchar(max),
DefSchemaName varchar(max),
UserId int,
SID varbinary(max)
)
insert into @groups exec sp_helpuser 'TheUser'
if exists (select * from @groups where GroupName = 'TheRole')
begin
print 'Removing user from role...'
exec sp_droprolemember 'TheRole', 'TheUser'
end
However, it doesn't hurt to just execute sp_droprolemember
regardless of whether the user is in the role.