<

[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x

SQL Server: "Error '15023' User or role already exists in the current database" when you perform restore database from backup

Published on
16,935 Points
10,735 Views
2 Endorsements
Last Modified:
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to allow necessary user permissions to the new database. This is puzzling enough since a database backup should bring us back to the original state. This is caused by Security Identification numbers (SID) that are mismatched or 'orphaned' in the sysusers table.

To resolve this problem, we can make use of the SQL Server stored procedure sp_change_users_login.

From MSDN, the syntax of sp_change_users_login is:

sp_change_users_login [ @Action= ] 'action'
    [ , [ @UserNamePattern= ] 'user' ]
    [ , [ @LoginName= ] 'login' ]
    [ , [ @Password= ] 'password' ]
[;]

1. Switch to the target database


use mydatabasename

Open in new window


2. List problematic SID


Lists the users and corresponding security identifiers (SID) in the current database that are not linked to any login. user, login, and password must be NULL or not specified. This step is informative and thus optional.

exec sp_change_users_login @Action='Report'

Open in new window


3. Fix problematic SID


Links the specified user in the current database to an existing SQL Server login. User and login must be specified. password must be NULL or not specified. The login name(s) to provide is/are from the result reported in step 2.

exec sp_change_users_login @Action='Update_One', @UserNamePattern='MyLoginID', @LoginName='MyLoginID'

Open in new window


Repeat running 'Update_One' statement for all login names.

4. Verify all is okay now


Repeat the sql statement with Action='Report' as described in step 2 to confirm we have resolved all login names. If no records are returned after running Action='Report', we are 99% there!

Now, try logging in using the "just-fixed" SQL user login id to confirm we have resolved "Error '15023'".

TIP: You may wish to save the your sql statements into a .sql file so you can reuse it in future database migration/restore.


MSDN Reference: http://msdn.microsoft.com/en-us/library/ms174378.aspx


To view the original article and discussion you can visit my blog here.


Cheers
hongjun
2
Comment
Author:hongjun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Expert Comment

by:Yashwant Vishwakarma
Very helpful article
Voted as Good Article :)
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Join & Write a Comment

In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month