When restoring a database backup to another server, you may experience a problem with orphaned users. This scenario displays and resolves the problem:
sp_addlogin 'janetl', 'dbo'
BACKUP DATABASE Northwind
TO DISK = 'c:\mssql7\backup\northwnd'
DROP DATABASE Northwind
sp_droplogin 'janetl'
RESTORE DATABASE Northwind
FROM DISK = 'c:\mssql7\backup\northwnd'
The janetl login is not allowed into the Northwind database unless the guest login is allowed. Even though the janetl login has been deleted, it still shows up (as an orphaned row) in the sysusers table:
USE Northwind
SELECT *
FROM sysusers
WHERE name = 'janetl'
To resolve orphaned users
sp_addlogin @loginame = 'nancyd',
@sid = 0x32C864A70427D211B4DD00104B9E8A00
sp_dropalias 'nancyd'
sp_dropuser 'janetl'
sp_droplogin 'nancyd'
BACKUP | sp_dropalias |
Backing Up and Restoring Databases | sp_droplogin |
RESTORE | sp_dropuser |
sp_addlogin |