The SQL Server error 15023 User already exists in current database occurs when a databases is restored from another instance. The database users aren’t mapped to the corresponding logins at the instance where it is restored and are termed as orphaned users. When one tries to login to the restored database with an orphan user we get message as “login failed for <user>”.  The fix is very simple.

Before we fix it, we need to get the list of orphan users. In order to replicate the issue, create a database user and then drop the login it is mapped too. Execute the below query to get it.

The above two queries return all database users which doesn’t map to any server login. The output is shown below.

sql server error 15023 user already exists in current database

Execute the below query to map the database user to the server login.

Query 1 uses the sp_change_users_login to map the user to a login with same name. This procedure will be deprecated in future. To get more on it refer to

Query 2 uses the ALTER USER method to map the user to login.

The above queries will fix one user at a time. In order to fix all orphan user in a database, execute the below query.

The above iterates through all the orphan users and maps them to login with same name.



Ahmad Osama

Like us on FaceBook Join the fastest growing SQL Server group on FaceBook

Follow me on TwitterFollow me on FaceBook