Brian Hartsock's Blog

SQL Server 2005: Backup, Restore, and Orphaned Users

by bhartsock on Oct.28, 2008, under Uncategorized

Backing up a SQL Server database and restoring it to another server is a common operation done for reporting, testing, or development. When restoring on a different machine, a common problem is the login principals don’t actually correlate to the restored database users. This is because they are linked by an SID. This SID isn’t the user name, its some hash value, probably a GUID. The problem is fully described in this article.

Fixing the problem is really simple. Just run the following SQL and substitute and with the appropriate values. I added these lines to all my Powershell scripts that are used to restore a database and it works like a charm.

USE <database_name>;
GO
sp_change_users_login @Action='update_one', @UserNamePattern='<database_user>',  @LoginName='<login_name>';
GO

Post to Twitter Post to Digg Post to Facebook Post to Reddit

:, , ,

2 Comments for this entry

  • Aleks

    This is so stupid, how long do you think this will take you if you have 300 users.

  • bhartsock

    You can script it in cases like that. Or, when creating users, you can specify the SID so they can be the same on every server so you don’t have this problem.

Leave a Reply

Post by day

October 2008
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031