User already exists in current database SQL Server 2005 Error 15023

Posted Monday, January 14, 2008 2:43 PM by Nathan Zaugg
I posted a blog entry a while back about how to change the owner of a database.  What that still does not solve is the problem you have when there is a database user in the database you restored and there is a database user already in your system, each with the same name.  While it seems intuitive that it should just work, it doesn't!  The user in the database is essentially an orphan.
 
To find such orphans in your newly-restored database we run this command:
 
EXEC sp_change_users_login 'Report'
 
This simply gives us a list of these orphan logins.  Generally you want to map the two users back to each other and we can do that with this command:
 
EXEC sp_change_users_login 'update_one', 'WebUser', 'WebUser'
 
You can run the first command again to see if it worked.  In my case I had to try to login a couple of times before the I could get it to login.  If that fails to work there is another, more powerful command you can run:
 
EXEC sp_change_users_login 'Auto_Fix', 'WebUser', NULL, 'MyWebUserPassword'
 
If you are too lazy to even do that, there are a few stored procedures on this blog entry that you can try running:
 
I hope this helps someone besides just me!  Drop me a line if this blog entry was useful.
 
 

Comments

# re: User already exists in current database SQL Server 2005 Error 15023

Wednesday, April 23, 2008 1:11 AM by Kipp

The important distinction is that if you already have a user by the same name then you need to use: EXEC sp_change_users_login 'update_one', 'WebUser', 'WebUser';  If you do not have a user by that name already in that database, you need to use: EXEC sp_change_users_login 'Auto_Fix', 'WebUser', NULL, 'MyWebUserPassword'

# re: User already exists in current database SQL Server 2005 Error 15023

Tuesday, October 28, 2008 7:42 AM by Chris Luebbe

Thank you!!!

# re: User already exists in current database SQL Server 2005 Error 15023

Wednesday, January 14, 2009 3:22 AM by Sham

Good blog.Thnx a lot

# re: User already exists in current database SQL Server 2005 Error 15023

Friday, July 31, 2009 4:06 PM by scripter

gr8 post, solved my problem with a preexisting login name. Thx a lot.