declare @username nvarchar(50)
create table #temp_user(
username nvarchar(50),
UserSID int
)
insert into #temp_user exec sp_change_users_login @Action='Report'
declare temp_cursor cursor for
select username from #temp_user
open temp_cursor
fetch next from temp_cursor into @username
WHILE (@@FETCH_STATUS=0)
begin
exec sp_change_users_login 'Auto_Fix', @username, NULL, @username;
exec sp_change_users_login @Action='update_one',@UserNamePattern=@username,@LoginName=@username;
fetch next from temp_cursor into @username
end
close temp_cursor
deallocate temp_cursor
drop table #temp_user