Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Logins

Author  Topic 

Jay1Jay
Yak Posting Veteran

50 Posts

Posted - 2005-07-14 : 16:20:33
I am currently working on migrating some sql2000 databases from Server A to Server B and having difficulties with transferring logins. My normal process is to backup the database, copy the .bak file over to new server and restore it that way. However, it only restores the databases and logins that are attached to those database but not the SQL Logins. Is there any stored procedures(query) that I can run to restore the logins and their passwords. Remember I do not want to restore the master database.

One way I was thinking was using DTS tool, however if my database is too big and my network speed between the 2 servers is not that great, what can I do.

Any help will be appreciated.

TimS
Posting Yak Master

198 Posts

Posted - 2005-07-14 : 16:54:14
SET NOCOUNT ON;
-- Script the logins run once on source & run script created on target.
select 'EXEC sp_addlogin @loginame = ' + name +
', @passwd = N''' + password +
''', @encryptopt = skip_encryption' +
char(13) + 'go'
from master.dbo.syslogins WHERE isntuser = 0 AND isntgroup = 0 AND sid <> 0x01


SET NOCOUNT ON;
-- Run on each target database to reattach users to login
select 'EXEC sp_change_users_login @Action = ''Auto_Fix'', @UserNamePattern = ''' + name + '''' +
char(13) + 'go'
from master.dbo.syslogins WHERE isntuser = 0 AND isntgroup = 0 AND sid <> 0x01

AS-IS NO WARRENTY

Tim S
Go to Top of Page

franco
Constraint Violating Yak Guru

255 Posts

Posted - 2005-07-15 : 04:17:50
Please refer to:
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q314546&SD=MSKB

Franco
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-07-15 : 08:25:56
My approach is similar to TimS's, but it doesn't try to preserve password etc. from the original server.

Run this on the database AFTER Restore, execute any of the generated SQL that is appropriate (it will generate code to DROP a user from the database [e.g. if it is no longer appropriate for them to have access on the new server], Create a Login for UserIDs that do not exist on the new server, and Re-attach a database user to a server login.

SET NOCOUNT ON
SELECT DISTINCT
CASE WHEN L.sid IS NOT NULL
THEN '-- User ' + COALESCE(U.[name], '[NULL]') + ' already exists on server'
ELSE 'EXEC sp_addlogin ''' + U.name + ''', ''password'', ''' + db_name() + ''''
+ CHAR(9) + '-- Only add if required!!'
END,
CHAR(13)+CHAR(10)+'-- EXEC ' + db_name()
+ '.dbo.sp_dropuser @name_in_db = '
+ '''' + U.name + ''' -- Remove if access no longer require to this DB',
CHAR(13)+CHAR(10)+'EXEC ' + db_name()
+ '.dbo.sp_change_users_login ''Update_One'', '
+ '''' + U.name + ''', '
+ '''' + U.name + '''',
CHAR(13)+CHAR(10)+ '-- ========== --'
FROM sysusers U
LEFT OUTER JOIN
(
sysmembers M
INNER JOIN sysusers G
ON G.uid = M.groupuid
) ON M.memberuid = U.uid
LEFT OUTER JOIN master.dbo.syslogins L
ON L.[name] COLLATE SQL_Latin1_General_CP1_CI_AS = U.[name]
WHERE U.islogin = 1
AND U.isaliased = 0
AND U.hasdbaccess = 1
AND
(
G.issqlrole = 1
OR G.uid IS NULL
)
AND U.name NOT IN ('dbo')

Kristen
Go to Top of Page

jpotucek
Constraint Violating Yak Guru

273 Posts

Posted - 2005-07-15 : 10:10:41
http://support.microsoft.com/kb/246133/ <--- How to transfer logins (with original SID and passwords)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-07-15 : 13:23:12
I'd just use Tim's script, slight modified to add the sid so that we don't need to use sp_change_users_login afterward.

Tara
Go to Top of Page
   

- Advertisement -