Assume that the name of SQL Login and Database Users are equal, I wrote a simple script for you. try this:
declare @user sysname;
declare @sql nvarchar(max);
set @user = 'test'
set @sql = 'declare @msg nvarchar(2048)'
select
@sql = @sql + char(13) +
'
if exists ( select
*
from
' + quotename(name) + '.sys.database_principals
where
type in ( ''S'', ''U'' )
and
name = ''' + @user + ''' )
begin
begin try
set xact_abort on;
begin tran
drop user ' + quotename(@user) + ';
print ''This user eliminated from the database '' + ''' + quotename(name) + '''
commit tran
end try
begin catch
if @@trancount > 0
rollback tran;
set @msg = ''There is a problem with eliminating this user from the database '' + ''' + quotename(name) + ''' ;
throw 60000, @msg, 1;
end catch
end
'
from
sys.databases
exec sp_executesql @sql