Advertisement
Guest User

SQL Server ntaccount translation

a guest
Sep 9th, 2011
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.10 KB | None | 0 0
  1. exec sp_configure 'allow_update',1
  2. reconfigure with override
  3. go
  4.  
  5. declare @SourceDomain nvarchar(255), @TargetDomain nvarchar(255)
  6. set @SourceDomain = 'INTERMARK'
  7. set @TargetDomain = 'CORP'
  8.  
  9. select name as SourceName, REPLACE(name,@SourceDomain + '\',@TargetDomain + '\') as TargetName,
  10.     sid as SourceSID, SUSER_SID(REPLACE(name,@SourceDomain + '\',@TargetDomain + '\')) as TargetSID
  11. into #Temp1
  12. from master..syslogins
  13. where (name like (@SourceDomain + '\%') or name like (@TargetDomain+'\%'))
  14.     and SUSER_SID(REPLACE(name,@SourceDomain + '\',@TargetDomain + '\')) is not null
  15.     and isntname = 1
  16.  
  17. declare Logins cursor local forward_only static
  18. for
  19.     select t1.TargetName
  20.     from #Temp1 t1
  21.     where not exists (select null from #Temp1 t2 where t1.TargetName = t2.SourceName)
  22.  
  23. open Logins
  24.  
  25. declare @logname nvarchar(255)
  26.  
  27. fetch next from Logins into @logname
  28.  
  29.     while @@FETCH_STATUS = 0
  30.     begin
  31.         exec sp_grantlogin @loginame = @logname
  32.         fetch next from Logins into @logname
  33.     end
  34.  
  35. close Logins
  36. Deallocate Logins
  37.  
  38. declare Dbs cursor local forward_only static
  39. for
  40.     select name
  41.     from sysdatabases
  42.     where name not in ('master','model','tempdb','msdb')
  43.     --where name in ('Northwind')
  44.  
  45. open Dbs
  46.  
  47.     declare @name sysname
  48.     fetch next from Dbs into @name
  49.  
  50.     declare @sql_text nvarchar(4000)
  51.  
  52.     while @@fetch_status=0
  53.     begin
  54.         select 'Обработка базы ' + @name + ':'
  55.  
  56.         set @sql_text = 'select ' + QUOTENAME(@name,'''') + ' as dbname, ' +
  57.             ' u.name as UserName, t1.SourceSID, t1.TargetSID from #Temp1 as t1 join ' +
  58.             QUOTENAME(@name) + '..sysusers u on t1.SourceSID = u.sid'
  59.  
  60.         execute(@sql_text)
  61.  
  62.          set @sql_text = 'update users '  +
  63.              ' set sid = t1.TargetSID' +
  64.              ' from ' + QUOTENAME(@name)+ '..sysusers as users ' +
  65.              ' join #Temp1 t1 on users.sid = t1.SourceSID '
  66.        
  67.         execute(@sql_text)
  68.  
  69.         fetch next from Dbs into @name
  70.     end
  71.  
  72. close  Dbs
  73. deallocate  Dbs
  74.  
  75. drop table #Temp1
  76.  
  77. exec sp_configure 'allow_update',0
  78. reconfigure with override
  79. go
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement