exec sp_configure 'allow_update',1 reconfigure with override go declare @SourceDomain nvarchar(255), @TargetDomain nvarchar(255) set @SourceDomain = 'INTERMARK' set @TargetDomain = 'CORP' select name as SourceName, REPLACE(name,@SourceDomain + '\',@TargetDomain + '\') as TargetName, sid as SourceSID, SUSER_SID(REPLACE(name,@SourceDomain + '\',@TargetDomain + '\')) as TargetSID into #Temp1 from master..syslogins where (name like (@SourceDomain + '\%') or name like (@TargetDomain+'\%')) and SUSER_SID(REPLACE(name,@SourceDomain + '\',@TargetDomain + '\')) is not null and isntname = 1 declare Logins cursor local forward_only static for select t1.TargetName from #Temp1 t1 where not exists (select null from #Temp1 t2 where t1.TargetName = t2.SourceName) open Logins declare @logname nvarchar(255) fetch next from Logins into @logname while @@FETCH_STATUS = 0 begin exec sp_grantlogin @loginame = @logname fetch next from Logins into @logname end close Logins Deallocate Logins declare Dbs cursor local forward_only static for select name from sysdatabases where name not in ('master','model','tempdb','msdb') --where name in ('Northwind') open Dbs declare @name sysname fetch next from Dbs into @name declare @sql_text nvarchar(4000) while @@fetch_status=0 begin select 'Обработка базы ' + @name + ':' set @sql_text = 'select ' + QUOTENAME(@name,'''') + ' as dbname, ' + ' u.name as UserName, t1.SourceSID, t1.TargetSID from #Temp1 as t1 join ' + QUOTENAME(@name) + '..sysusers u on t1.SourceSID = u.sid' execute(@sql_text) set @sql_text = 'update users ' + ' set sid = t1.TargetSID' + ' from ' + QUOTENAME(@name)+ '..sysusers as users ' + ' join #Temp1 t1 on users.sid = t1.SourceSID ' execute(@sql_text) fetch next from Dbs into @name end close Dbs deallocate Dbs drop table #Temp1 exec sp_configure 'allow_update',0 reconfigure with override go