Click here to Skip to main content
Licence 
First Posted 4 Nov 2003
Views 72,423
Bookmarked 29 times

SQL Server Permissions - Fixing Orphaned Users

By | 4 Nov 2003 | Article
A short article on recovering orphaned users when moving a database from one server to another.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This short article is on recovering orphaned users when moving a database from one server to another. It took me quite a while to find a good fix, and once I had, it seemed only fair to share it with fellow CPians:

Sometimes it is necessary to move a database from one server to another. In my case we decided to place a "disaster recovery" machine in a building in another city, and copy various things, including our database across every night.

This works great, but we couldn’t use the website which is dependant on the database on this new server. The ASP error coming back was that the database required could not be opened.

The Problem

On the "Security" tab of our SQL server, the appropriate user existed. But when I went into the individual databases and looked inside the "User" tabs, the users (which had been there yesterday, before the database was copied during the night) were no longer there. "Not a problem", I thought – right click, "New Database User", select the user and click OK. At this point, the user was not added, but the following message was displayed:

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

Rather annoying, since the user was clearly not listed.

After some reading, I found that the problem occurs as each user name is stored in the syslogins table with a unique "SID" (for SQL 2000 – SUIDs for SQL 6.x or earlier) on each server. When a database is copied to a new server the SIDs for the usernames no longer match.

The fix I found is as follows:

  1. Open "Query Analyser".
  2. Run the following stored procedure:
    "sp_change_users_login 'auto_fix', 'UserName'"

    where UserName is the user name you need fixed.

  3. Go back to "Enterprise Manager", right click on the database and select "Stop". Once it’s stopped, right click again and select "Start".
  4. Return to the “Users” tab on your database – I found it wasn’t necessary to re-add the user at all – it had been added already.

* Note that the execute permissions will need to be reset for any stored procedure called using this username.

Well, that's the problem and the fix as I found it. Hopefully this will help to make someone else's day less bewildering than my morning was.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Megan Forbes

Web Developer

Australia Australia

Member

I am a South African web / SQL developer currently spending my time as a mum. My other passion is photography and have put a small portfolio up here[^]

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBrilliant, cheers PinmemberPGMIKET18:13 28 May '07  
JokeThanks, Megan!!! PinmemberJulien Berube9:42 11 Jan '07  
GeneralSQL2005 [modified] Pinmemberjn1484:03 21 Nov '06  
GeneralSaved Again PinmemberJohnDeHope33:44 9 Nov '06  
GeneralMany Thanks Pinmembervirsum10:05 22 Mar '05  
GeneralRe: Many Thanks PinsussMartin Gleeson16:08 27 Apr '05  
GeneralExporting MSSQL users PinsussAnonymous6:22 20 Jan '05  
GeneralWorks Great, but PinmemberT. Bradley Dean19:28 27 Jul '04  
GeneralCool! I'm curious about one thing. PinmemberJeff Varszegi7:26 10 Nov '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 5 Nov 2003
Article Copyright 2003 by Megan Forbes
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid