The next piece of code helps fix orphaned users by reconnecting them to logins that have precisely the same name, but a differing SID. This code is a variant of the above code that dynamically creates
ALTER USER
statements. A statement is created for each orphaned user where there is a match-by-name in the list of server logins. Once the list of dynamically createdALTER USER
statements are compiled, the commands to fix orphaned users are automatically executed.
Click through for the scripts.