Archive for October, 2010

Mapping orphaned database users to SQL Server 2005 logins

SQL Server | Posted by p_lider October 4th, 2010

If you have many users defined in your MS SQL Server 2005 database, then if you will backup your database and then restore in a new, clean SQL Server instance, you will not be able to map database users with logins in your SQL Server instance. This is because of different SID numbers between the source and destination SQL Server instances. So even creating logins named exactly the same as users in a restored database will not help.

Fortunately, I found a solution. The SQL scipt below will map orphaned users in databases to logins in SQL Server instance even when their SIDs do not match:

sp_change_users_login @Action=’update_one’,
@UserNamePattern=’<database user name>‘,
@LoginName=’<sql server instance login name>
GO