Chris's profileChris Burrows' Live Spac...PhotosBlogListsMore Tools Help

Blog


    September 13

    Error user doesn't exist in database after restoring a database with SQL User Accounts.

    If you SQL Accounts on your database (not something that I would recommend in production) and you are restoring databases you are proberbly aware of the following above error.    Now if you are like me the first thing that you do is check to see if the user exists in the database.  If you are restoring the db, most of the time it does.   It then takes a couple of seconds for me to remember that if you are restoring a database from a different server then the link between the SQL Server account and the User in the database doesn't exists.  So even when you open up the database and see the user you cannot still login.   The way you normall fix this is by deleting the user from the database, then going back to the user underneath SQLSecurity and granting access to the database for that user again.  After doing this a couple of times I got sick of it and created the following script. 

    DECLARE @USERNAME varchar(8000)
    SELECT @USERNAME = 'fred'
    if not exists (select * from dbo.sysusers where name = @UserName)
    BEGIN
        exec sp_grantdbaccess @USERNAME , @USERNAME
        EXEC sp_addrolemember 'db_owner',@USERNAME
    END
    ELSE
    BEGIN
        EXEC sp_change_users_login 'Update_one',@USERNAME ,@USERNAME
    END

    What this does is grant access to the current database to the given username specified and then adds then to the role db_owner.   I have another script which will restore the database as well after I've santised it I'll post it.

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://madtechnology.spaces.live.com/blog/cns!F637E4FD356E127D!127.trak
    Weblogs that reference this entry
    • None