title: How to map all the orphan users in sql server management studio express 2008
body: <p>my target was to moved all the mssql databases from sql server management studio express 2005 server to sql server management studio express 2008  server. For this i first created the databases and the users in the new server via plesk panel. I tried logging in to the database with the created users and was able to login without any issues. But after i restore the database from the old server i was not able to login to the same database. It throws the following error: </p>
  
  <p>Create failed for User 'USER_NAME'. (Microsoft.SqlServer.Smo)
  User, group, or role 'USER_NAME' already exists in the current database. (Microsoft SQL Server, Error: 15023)</p>
  
  <p>I googled it and found the following sql query which fixed the problem, but one database at a time. 
  Here is the query syntax</p>
  
  <p>USE DB_NAME<br><br>
  GO<br>
  EXEC sp_change_users_login 'update_one', 'USER_NAME', 'LOGIN_NAME’<br>
  GO<br></p>
  
  <p>I have 100s of database and i can't do this manually for all the database. My question is.. is there any alternative to map all the orphan users to their databases?</p>
  
  <p><strong>Update - **<br>
  **Cause of this issue</strong></p>
  
  <p>So as far as my research goes, the issue is with the SID (  ) override after restoring the database from the source server. </p>
  
  <p>Initially while creating new logins it will be assigned with a SID and once we restore the database from source server, the login will be having the new SID assigned to it by the new server and the database will have the old SID from the source server which causes the SID mismatch between the login and the database. To resolve this we use the above syntax to override the SID in the database with the SID of the login user, that way both database and login will have the same SID.</p>
  
  <p>Reference: <a href="http://dbadiaries.com/how-to-transfer-logins-to-another-sql-server-or-instance" rel="nofollow noreferrer"> here </a></p>