declare @user varchar(20)
declare @fix_ou varchar(100)
declare c1 cursor for select d.name --,d.sid,s.name,s.sid
from sys.database_principals d
left join sys.server_principals s
on d.name=s.name
where d.type_desc='SQL_USER'
and d.name not in ('dbo','guest','INFORMATION_SCHEMA','sys','MS_DataCollectorInternalUser')
and d.principal_id >4
and s.name is not NULL
and d.sid!=s.sid
and authentication_type_desc = 'INSTANCE';
open c1
fetch next from c1 into @user
while @@FETCH_STATUS=0
begin
set @fix_ou='alter user ['+@user+'] with login =['+@user+']'
--set @fix_ou='exec sp_change_users_login ''auto_fix'','+@user
exec(@fix_ou)
print 'fixed user: '+@user
fetch next from c1 into @user
end
close c1
deallocate c1
标签:name,批量,fix,sid,server,user,sql,c1,ou
From: https://www.cnblogs.com/ls11736/p/18191508