Fix SQL Server Error 15023 (User Already Exists)
SQL Server error 15023 says a user already exists in the database when you try to add it. A restore or an attach on a new server sets up this error, because the old user stays in the database without a matching login.
This guide shows how to find the orphaned user and gives four fixes in the order that costs you the least.
Quick SummarySQL Server error 15023 means the database already holds a user with that name. The user is orphaned when its SID matches no login on the server. Find orphaned users with a query on sys.database_principals. Map the user to a login with ALTER USER WITH LOGIN. Create the login again with the original SID when no login exists. Move to contained database users before the next restore on another server.
What Does SQL Server Error 15023 Mean After a Restore?
Error 15023 says the user, group or role already exists in the current database. SQL Server shows it when you create a user or map a login to a user name that the database already holds.
Msg 15023, Level 16, State 1, Line 1
User, group, or role 'AppUser' already exists in the current database.
Microsoft explains the state behind the message. A database user is orphaned when it links to a login SID that does not exist in the master database. A restore or an attach on a server without that login creates this state. A dropped login does the same, and a new login with the same name gets a different SID so the user stays orphaned.
Do You Know?A backup file holds the database users and does not hold the server logins. Move the data with migrate a SQL Server database to a new server and plan the logins in the same step. Follow attach vs restore database when you choose between the two paths.
How to Find the Orphaned Users in a Database?
Execute the query from Microsoft in the user database. It returns the SQL Server authentication users whose SID has no login on the server.
SELECT dp.type_desc, dp.sid, dp.name AS user_name
FROM sys.database_principals AS dp
LEFT JOIN sys.server_principals AS sp
ON dp.sid = sp.sid
WHERE sp.sid IS NULL
AND dp.authentication_type_desc = 'INSTANCE';
Expected output has one row for every orphaned user. An empty result means no SQL Server authentication user is orphaned. The query covers the authentication type INSTANCE only, so check Windows users by looking for their login under Security in Object Explorer.
| What you find | Cause | Start with |
|---|---|---|
| A login with the same name exists on this server | Same name and a different SID | Method 1 |
| No login exists and you hold the old SID from the query | Login never created on this server | Method 2 |
| No login exists and the user needs fresh permissions | Old user has no use on this server | Method 3 |
| The database moves between servers again and again | Server logins tie the database to one instance | Method 4 |
How to Fix SQL Server Error 15023?
Every example uses the database OrdersDB and the user AppUser. Replace the names with your own values.
Method 1: Map the User to an Existing Login With ALTER USER
Use this method when a login with a matching name exists on the server. Microsoft documents ALTER USER with the LOGIN option as the way to map an orphaned user to a login in master. Execute it in the user database.
USE [OrdersDB];
ALTER USER [AppUser] WITH LOGIN = [AppUser];
Older scripts call the procedure sp_change_users_login. Microsoft lists that procedure as deprecated and gives a replacement for every action.
| Old call | Replacement |
|---|---|
| sp_change_users_login ‘Report’ | The detection query from the section above |
| sp_change_users_login ‘Auto_Fix’, ‘user’ | ALTER USER user WITH LOGIN = user |
| sp_change_users_login ‘Update_One’, ‘user’, ‘login’ | ALTER USER user WITH LOGIN = login |
Method 2: Create the Missing Login With the Original SID
Use this method when no login exists on the server. Copy the SID from the detection query and create the login with that exact value. Execute the statement in master.
USE [master];
CREATE LOGIN [AppUser]
WITH PASSWORD = N'ChooseAStrongPasswordHere1!',
SID = 0x1A2B3C4D5E6F708192A3B4C5D6E7F801;
Replace the password and the SID with your values. The user signs in with the password you set and can change it afterward with ALTER LOGIN. Microsoft states that a login created with a different SID leaves the user orphaned, so the SID value must match.
Method 3: Drop the User and Create It Again
Use this method when the old user has no value on the new server. Practitioner guides use it widely, and Microsoft does not document it as a fix. The user loses every permission inside the database, so list the permissions first.
SELECT USER_NAME(grantee_principal_id) AS user_name, permission_name, state_desc, OBJECT_NAME(major_id) AS object_name
FROM sys.database_permissions
WHERE USER_NAME(grantee_principal_id) = N'AppUser';
Save the output and drop the user. Create the user again for the login and grant the permissions back.
DROP USER [AppUser];
CREATE USER [AppUser] FOR LOGIN [AppUser];
SQL Server blocks the drop with error 15138 when the user owns a schema. Move the schema to dbo with ALTER AUTHORIZATION before you drop the user.
Method 4: Move to Contained Database Users
Use this method when the database moves between servers. Microsoft recommends contained database users for databases that you plan to move. A contained user signs in at the database level, so no server login ties it down and it cannot become orphaned.
sp_configure 'contained database authentication', 1;
RECONFIGURE;
ALTER DATABASE [OrdersDB] SET CONTAINMENT = PARTIAL;
Convert the user after the database accepts partial containment. Execute sp_migrate_user_to_contained in the contained database and keep the user name.
USE [OrdersDB];
EXECUTE sp_migrate_user_to_contained
@username = N'AppUser',
@rename = N'keep_name',
@disablelogin = N'do_not_disable_login';
Heads UpMicrosoft states that sp_migrate_user_to_contained cannot be reversed. A contained database also does not support replication, change data capture or change tracking. Check the features of your database before you convert it.
Error 15023 Questions Answered
What Is an Orphaned User in SQL Server?
An orphaned user is a database user whose SID matches no login in the master database. The user exists in the database and cannot sign in to the instance.
Does Creating the Login Again Fix Error 15023?
Only when the new login uses the original SID. Microsoft states that a login with the same name and a new SID leaves the user orphaned.
Is sp_change_users_login Safe to Use Now?
Microsoft lists sp_change_users_login as deprecated and says a future version can remove it. Use ALTER USER with the LOGIN option instead.
Does the Detection Query Find Windows Users?
No. The Microsoft query returns users with the authentication type INSTANCE. Check Windows users by looking for their login under Security in Object Explorer.
Does a Restore Carry the Server Logins?
No. The backup holds the database users and the permissions inside the database. Create the logins on the new server or move to contained database users.
How to Avoid Orphaned Users on the Next Move?
Create the logins on the new server with the original SIDs before you restore the database. The users then match their logins from the first connection.
Use contained database users for databases that move often. Test the sign in of every application account right after the restore.
Who, How and WhyAndrew Jackson, Technical Content Specialist, wrote this guide. The definition of an orphaned user, the detection query, the ALTER USER and CREATE LOGIN fixes and the contained user steps follow the Microsoft Learn pages linked in the text. The message text of error 15023 comes from public sources because Microsoft has no page for it. The drop and create method comes from practitioner guides and is not a Microsoft fix. The T-SQL was checked for syntax against Microsoft documentation and was not executed on a live server. The guide exists because a restore on a new server leaves users without logins and the message does not say so.
Key Takeaways
- SQL Server error 15023 means the database already holds a user with that name.
- An orphaned user links to a login SID that does not exist in master.
- ALTER USER WITH LOGIN maps the user to a login that exists on the server.
- CREATE LOGIN with the original SID fixes the user without touching the database.
- sp_change_users_login is deprecated and ALTER USER replaces every action.
- Contained database users cannot become orphaned.