Reconnecting orphaned users

If you are restoring a SQL Server database that has user IDs in it between machines, the user IDs from the source machine won't match the destination machine. As a result, you need to correct these “orphans” using this little bit of T-SQL code:

 

EXEC sp_change_users_login 'Report' -- list orphan users. 
EXEC sp_change_users_login 'Auto_fix', 'username' -- autocorrect the orphan user . 
EXEC sp_change_users_login 'Report' -- list orphan users to verify that they are no longer orphans.

 

Tag(s): SQL Server | T-SQL

An error has occurred. This application may no longer respond until reloaded. Reload 🗙