Sign up ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I occasionally move a database (SQL Express 2012) from a development machine to a server, or vice versa, using database backup and restore in SQL Server Management Studio.

Whenever I do this, the applications on the target machine cannot access the database until I delete the user they use, "george", from the database users (Database, Security, Users in SQL Server Management Studio), and re-add it as owner under Security, Logins, george/properties, user mapping.

Is there a better way to do this? It seems a little convoluted.

share|improve this question
The SIDs of the logins don't match between the servers. Manually specify the SID in the CREATE LOGIN statement. –  Jon Seigel Apr 24 '13 at 22:44

2 Answers 2

up vote 12 down vote accepted

This is the difference between logins and users and how they relate to each other:

  • Logins - Instance level principals that allow an entity to connect to the SQL Server instance. They do not, by their nature, grant any access to databases on the instance. The exception to this is a login with sysadmin rights can use a database because they are sysadmin, but because of sysadmin level permissions.
  • Users - Database level principals that allow an entity to connect to a SQL Server database. Users are associated with logins via SIDs, creating a relationship between the two and allowing a login to connect to the instance and then use the associated user to connect to the database.

What commonly happens with SQL authenticated logins and database users on a restore is that the SIDS will be out of sync, thus breaking the relationship. This relationship must be repaired before you can connect to the database using that login, because in the eyes of SQL Server those principals are no longer connected. You can fix this with the following SQL:


You can use the following query in the context of your database to check for orphans:

select [user_name]
    ,dp.type_desc [user_type]
    ,isnull(,'Orhphaned!') [login_name]
    ,sp.type_desc [login_type]
    sys.database_principals dp
    left join sys.server_principals sp on (dp.sid = sp.sid)
    dp.type in ('S','U','G')
    and dp.principal_id >4
order by
share|improve this answer
Thanks Mike this script shows the users of the data base, but how to copy the users to logins ? –  Ahmad saleh Aug 5 at 8:43

You could look into changing the database to contained database. Contained database user are authenticated by the database, not at instance level through login. It makes moving database to different instance simpler.

If not, you could backup the login information using sp_help_revlogin scripts provided at this Microsoft support KB. And execute the output script on the new instance.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.