Mike's Blog


Business Intelligence keeps me live and communicating!

SQL Server Tip - Fixing Orphaned Users

How often do you bump into problems with users and security when you move a SQL Server database to another server. The most annoying problem is when you try to create a user under Security, and the user exists within the moved database. When you try to apply user mapping and permissions you get an error like this:

Error 15023: User, group or role '%s' already exists in the current database.

Normally you don't have to reapply the user permissions because SQL Server uses a special background process to associate user accounts in a database with logins on the server. This could take some time, but luckily there’s a quick way to do this:

use database
go

sp_change_users_login 'auto_fix', 'UserName'

This will output the following result.

The row for user ' ' will be fixed by updating its login link to a login already in existence.
The number of orphaned users fixed by updating users was 1.
The number of orphaned users fixed by adding new logins and then updating users was 0.

Microsoft has a knowledge base article that explains the problem and solution Q240872.

 

Technorati tags:

Comments

Lise said:

You are my hero. :)

# February 3, 2009 8:50 PM

jimmy said:

thank you very much for this sp_change_users_login trick !

it helped me today !!!

# March 19, 2009 3:56 PM

Stefan said:

Thanks so much for posting.  This helped me out in a pinch.

# March 24, 2009 11:05 PM

ctrnz said:

Helped me a lot.

Thank You!

# June 15, 2009 2:40 PM

Daniel said:

Thank you. It works like a charm!

# March 16, 2010 7:52 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 3 and 2 and type the answer here: