I have sql server that has local windows users as logins.
I want to create a duplicate of this setup in the development environment. The dev environment server will have the same computer name and will have the same local users created in windows.
If I script the logins and deploy them on the dev server, then the users will get orphaned since the login sid will not match the user sid in the db.
So instead, I am thinking about backup/restore of master db from the prod sql server into the dev sql server (both same version), to bring in the logins, followed by restoring the user dbs. This will ensure that dev server has same configuration settings and the logins have the same sid as the user databases.
However, I also realize that the actual sid of local windows user (in windows) on the new computer is different, and so the local windows login fails. To solve this problem I will need to script that login, delete the login, run the script to create the login, and fix the orphaned user. Have I got this right or is there another workaround?
1 Answer 1
Did you try the ALTER USER ... WITH LOGIN ... command?
The documentation say that you can use this for not only SQL logins but also Windows users to make its SID match the desired login's SID.
-
Your answer is to alter the db user. But the login itself will be from the old server so I will need to delete it, recreate it, and fix the orphaned db user. No?variable– variable2022年09月09日 10:45:33 +00:00Commented Sep 9, 2022 at 10:45
-
Ah, yes I see now that you restore the master database. I typically don't do that. Yes, you have to re-create the login, since it points to the wrong SID. And then map the user to the right login.Tibor Karaszi– Tibor Karaszi2022年09月09日 13:15:19 +00:00Commented Sep 9, 2022 at 13:15
-
To map, is using the ALTER USER command you have given above same as how sp_change_users_login works?variable– variable2022年09月09日 15:06:03 +00:00Commented Sep 9, 2022 at 15:06
-
Yes, those commands do the same thing. I prefer the newer command compared to the old deprecated command. That is why I recommended ALTER USER.Tibor Karaszi– Tibor Karaszi2022年09月11日 14:59:27 +00:00Commented Sep 11, 2022 at 14:59
CREATE LOGIN ... FROM WINDOWS
andCREATE USER ... FROM LOGIN
.