Problem

When restoring/moving databases, the link (aka SID) between logins and database users might get broken, effectively preventing login from being able to access databases even if no names change has occurred. This situation is commonly known as orphans users, i.e. users that don’t have a parent, meaning corresponding login.

Solution

Link back any non-disabled logins (server-level) with users (database-level) based on the same name. The same solution could be used to allow linking logins and users that do not share an exact name.

https://github.com/steverezhener/DBA/blob/master/Procedures/security.usp_fix_database_orphan_users.sql