SQL Error 15023: User already exists in current database

Error 15023: User already exists in current database.


Cause:

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 and are considered as orphaned users.

Solution:


Step 1: The first thing we need to do is get a list of orphan users for the target database.
 USE DatabaseName  
 GO  
 EXEC sp_change_users_login 'Report'  
 GO  

Step 2: Run the following to associate login with the username for each orphaned user. The ‘Auto_Fix’ attribute will create the user for the SQL Server instance if it does not already exist. In following example ‘LoginName’ is UserName, and ‘P@ssw0rd1’ is Password. Auto-Fix links a user entry in the sysusers table in the current database to a login of the same name in sysxlogins.


 USE DatabaseName  
 GO  
 EXEC sp_change_users_login 'Auto_Fix', 'LoginName', NULL, 'P@ssw0rd1'  
 GO  

No comments

Back to Top