Transfer SQL Server Logins without Breaking Database User Mappings
Restoring or attaching a database moves its database users, but it does not automatically create matching server logins. For instance-authenticated users, SQL Server associates the two through a security identifier, or SID. Recreating a login with the same name and a different SID can still leave the user orphaned.
Inventory the Required Identities
On the source instance, use an appropriately authorized administrator account to inspect the intended logins:
SELECT name, type_desc, sid, is_disabled, default_database_name
FROM sys.server_principals
WHERE type IN ('S', 'U', 'G')
AND name = N'ArticleReader';
Replace ArticleReader with one explicitly selected login. Record its database access and required server permissions. Inventory Windows groups separately from SQL authentication logins. Contained users are a different model and do not require the same instance-login transfer.
Generate and Review the Transfer Script
Use the current Microsoft login-transfer procedure linked below. Its sp_help_revlogin helper is a script you install from that article, not a built-in procedure guaranteed to exist. After reviewing and installing that helper on the source as documented, generate only the required login:
USE master;
GO
EXEC dbo.sp_help_revlogin @login_name = N'ArticleReader';
The generated SQL carries the identity information needed for migration. Treat password hashes as secrets. Store the output in restricted temporary storage, transfer it through an approved channel and remove the temporary copy after verification.
Review the output for name/SID conflicts, disabled state, policy settings and permissions before executing it on the destination. Check password-hash compatibility between the exact source and destination versions. Do not assume a hash produced by a newer engine can be consumed by an older one, and do not drop an existing destination login automatically to resolve a collision.
Verify the Destination Mapping
In each migrated user database, compare the database and server principals:
SELECT dp.name AS DatabaseUser, sp.name AS ServerLogin,
dp.sid AS UserSid, sp.sid AS LoginSid
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp ON sp.sid = dp.sid
WHERE dp.name = N'ArticleReader';
The intended server login should appear, and both SIDs should match. If the destination intentionally uses a different existing login, remap the user with ALTER USER ... WITH LOGIN after confirming the identity rather than deleting the database user.
Test Real Access
Test a new connection as the migrated identity and perform a representative permitted operation. Verify that prohibited operations remain denied. Review default databases, role memberships, explicit grants/denies, jobs and credentials independently. A login transfer is not a complete server migration, and a successful administrator query does not prove the application's access.
Microsoft References
Author: Herman Schoenfeld