Richard Fryar (2/8/2013)
Often, you find yourself failing over several databases, or several hundred in a real disaster scenario.This script checks all databases in one go, and avoids use of a cursor.
exec sp_msforeachdb 'use [?];
declare @sql varchar(max)
select @sql = s from (
select ''use ['' + db_name() + '']; exec sp_change_users_login ''''Update_One'''', '''''' + d.name + '''''', '''''' + d.name + '''''';''
from sys.database_principals d
join sys.server_principals p on d.name = p.name
left join sys.server_principals o on d.sid = o.sid
where o.name is null
and d.name not in (''public'')
for xml path('''')) x(s);
if @sql is not null exec(@sql)
'
Thanks Perry for explaining.
Richard, Can you please explain the script above, if you don't mind. Is it going through each database that I failover and checking for orphan users and fixing them at the same time?
SueTons.
Regards,
SQLisAwe5oMe.