Database users gone after restore on different server

Posted on 2005-04-26
Last Modified: 2010-03-19
I am working on a database consolidation project and just migrated the first DB using a backup/restore process. I then noticed that the users section of that DB was empty and users could not connect to the DB. I checked it through Ent. Manager on the server itself and the users were there but it wouldnt show on my client system through Ent. Manager. I deleted and readded the users and now it works and all users can connect again. I did a test with another DB and the same thing happened. What could be causing this? Thanks in advance for any help.
Question by:acompani
    LVL 68

    Accepted Solution

    You have to "re-sync" the userid in the db with the login on the server.  This is done using command "EXEC sp_change_users_login".

    Run the statement below in Query Analyzer and it will generate the change commands you need; then copy and run them in QA also.

    SELECT 'EXEC sp_change_users_login ''UPDATE_ONE'', ''' + + ''', ''' + + ''''
    FROM sysusers usr WITH (NOLOCK)
    LEFT OUTER JOIN master.dbo.syslogins [log] WITH (NOLOCK) ON usr.sid = [log].sid
    WHERE usr.hasDbAccess > 0 AND usr.isNtName = 0 AND [log].sid IS NULL
    LVL 68

    Expert Comment

    The userids aren't so much gone as orphaned.

    For example, before running any other commands, go into QA in that db and issue this command:

    EXEC sp_helpuser

    You should see all the users, but the LoginName column is NULL; that prevents SQL from recognizing them.  The change command updates that column to the correct value.

    Author Comment

    The result of the initial query was a column called 'No Column Name' with no value. I ran it against Master and recieved an EXEC sp_helpuser command for the guest account but nothing for DB I moved over. Thanks for your help.

    Author Comment

    I went ahead and did:

    USE DBname
    EXEC sp_changedbowner 'sa'

    It then mapped sa to the db owner orphaned login.

    Thanks for your help, it got me what I needed.
    LVL 68

    Expert Comment

    D'OH, yeah, I should have mentioned that the commands above need to be run from within the specific user db, not from master db.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now