Nested Cursor for sp_change_users_login

Every DBA knows this Error:

"Error 15023: User already exists in current database."

For example: When I backup some database from production environment and then I want to restor it in a testing environment and in the end I need to set permission for some users, this error appears "15023".

How to fix this?

We can use system stored procedures "sp_change_users_login".

Therefore, I created Nested Cursor, that generates sp_change_users_login for every database and for each users. It looks like this:

sp_change_users_login | Auto_Fix

This script saves me a lot of time because I restore some DB on TEST, DEV or UAT environment very often.

As you can see below:

I hope this article helps!