How to fix orphaned SQL Server users

When you restore a Microsoft SQL Server database on a different machine, you cannot access the database until you fix the permissions. The solution is of course common knowledge now, but I can never remember it and always have to search, so I’m posting here so I know where to find it:

The problem is that the user in the database is an "orphan". This means that there is no login id or password associated with the user. This is true even if there is a login id that matches the user, since there is a GUID (called a SID in Microsoft-speak) that has to match as well.

In SQL Server 2000 SP3 and later there is a stored procedure that does the work:

All of these instructions should be done as a database admin, with the restored database selected.

First, make sure that this is the problem. This will lists the orphaned users:

EXEC sp_change_users_login 'Report'

If you already have a login id and password for this user, fix it by doing:

EXEC sp_change_users_login 'Auto_Fix', 'user'

If you want to create a new login id and password for this user, fix it by doing:

EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'

Comments

James Perrins

re: How to fix orphaned SQL Server users

08 March 2012

Well - you live and learn - I've always just deleted and recreated - but that looks easier if you have complex permissions or lost of users

Comments are closed on this post.
Find out more