Remap an Orphaned Database User to the Correct SQL Server Login
A database user and a server login are distinct principals. Their names can differ, and matching names alone do not establish a mapping. After a database move, use ALTER USER ... WITH LOGIN to connect an existing instance-authenticated database user to the correct login while retaining that user's permissions.
Diagnose in the Affected Database
Connect to the affected user database with an account allowed to inspect the relevant metadata:
SELECT DB_NAME() AS DatabaseName;
SELECT dp.name AS DatabaseUser, dp.type_desc, dp.sid
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp ON sp.sid = dp.sid
WHERE sp.sid IS NULL
AND dp.authentication_type_desc = 'INSTANCE';
The result identifies instance-authenticated users without a visible matching login. Incomplete metadata permissions can make diagnosis misleading. A login failure can also have causes unrelated to orphaning, such as a disabled login or unavailable default database.
Confirm the Intended Login
SELECT name, type_desc, sid, is_disabled
FROM sys.server_principals
WHERE name = N'ApplicationReader';
Confirm that this is the correct identity. Do not remap a user to whichever similarly named login happens to exist. Where you are migrating SQL logins, preserving the original SID during transfer can prevent the problem in the first place.
Rehearse the Remapping
Replace both example names. Run this in a new session without an existing transaction, in the affected user database:
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
ALTER USER [ArchiveReader] WITH LOGIN = [ApplicationReader];
SELECT dp.name AS DatabaseUser, sp.name AS ServerLogin
FROM sys.database_principals AS dp
JOIN sys.server_principals AS sp ON sp.sid = dp.sid
WHERE dp.name = N'ArchiveReader';
ROLLBACK TRANSACTION;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
THROW;
END CATCH;
During the transaction, expect ArchiveReader to map to ApplicationReader. The rollback deliberately leaves the original mapping intact. Once the pairing is confirmed, execute the single ALTER USER statement as the approved change and verify a fresh connection as the application identity.
Keep the Operation Narrow
This changes the user-to-login mapping. It does not rename a login or reset its password. Do not drop and recreate the database user merely to fix its SID, as that can disrupt permissions and ownership. Users without logins, contained users and certificate/asymmetric-key users need their own handling. The clause also cannot convert a Windows user into a SQL-authentication user.
The older sp_change_users_login procedure is deprecated. Use the explicit mapping syntax above for current SQL Server work.
Microsoft References
Author: Herman Schoenfeld