-------------------------------------------------------------------------------------
-- extracting configuration information
sp_configure 'show advanced options', 1
go
reconfigure with override
go
sp_configure
go

-------------------------------------------------------------------------------------
-- backing up syslogins table
exec master..xp_cmdshell 'bcp master..syslogins out c:\temp\syslogins.bcp -c -Usa -P'
go

-------------------------------------------------------------------------------------
-- restoring syslogins table
exec master..sp_configure 'allow updates', 1
go
reconfigure with override
go
-- create a table with the same 
-- structure as syslogins
select * 
into   ##logins 
from   master..syslogins 
where  1=0

-- restore syslogins into a temporary table
exec   master..xp_cmdshell 
  'bcp tempdb..##logins in 
  c:\temp\syslogins.bcp -c -Usa -P'

-- report logins that cannot be restored
select 'cannot setup login', a.name, 
       'because suid is used by', b.name
from   ##logins a, master..syslogins b 
where  a.suid = b.suid 
and    a.name != b.name

-- restore syslogins
insert master..syslogins 
select * 
from   ##logins a 
where  not exists ( 
    select * 
    from   master..syslogins b 
    where  b.suid = a.suid )
go
-------------------------------------------------------------------------------------
