Jan. 13, 2016, 9:53 a.m.

Fix Microsoft SQL Server User Login Bindings

If you ever tried to backup / restore a Microsoft SQL Server database from one server to another, or one database instance to another, you might have run into the issue where the database security user account has been restored but it does not have a corresponding instance login account, or, the instance login account is present but it is not linked to it. You may be tempted to open up the instance login account and assign it to the database and database user account, but you will receive an error:

User, group, or role 'XXX' already exists in the current database (Microsoft SQL Server, Error: 15023)

The fix is simple - run this from an SQL window:

EXEC sp_change_users_login 'Auto_Fix', 'UserLoginAccountName', NULL, 'UserLoginAccountPassword';