in Microsoft SQL Server

Re-map user to login in SQL server after restoring or attaching database

Re-map database user to a login in SQL server after restoring or attaching database

When restoring or attaching a database to a new or different Microsoft SQL server the logins and mappings to the database users are lost. If you create the logins again you’re not able to map the users because they already exist in the database:

User, group, or role '{login}' already exists in the current database.

Here’s the solution:

1. Create the logins with correct passwords

2. Open a new query window and execute this query

USE {database};
ALTER USER {user} WITH login = {login}

{database}: The database containing the orphan user
{user}: The orphan user name
{login}: The login name. You can use the same login as used on the old server or map the user to a different login name

Write a Comment

Comment

  1. Thanks!.. I finally manage to map my users to their respective login.

  2. Aw, this was a very good post. Taking the time and actual effort to make a very
    good article… but what can I say… I put things off a whole lot and don’t seem to get
    nearly anything done.