Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking ALL Users to Login after restoring a SQL Server 2005 database

(Note this question asks about linking ALL Users, unlike the possible duplicate that asks about linking a single user)

I wish to move a database between two servers, I have backed the database up from the first server and done a database restore on the 2nd server, so far so good.

However our application makes use of a lot of database users that are defined in the database. These have to be linked to logins that are defined in the master database. The server I have restored the database to has all the logins defined, however they have different sids.

I am not a T-SQL expert….

I think sp_change_users_login is part of the solution, but I can't find out how to get it to automatically link all users in the restored database to the login of the same name.

The database creation scripts we use for our application create the users and logins, however it does not specify the SID when creating the login, hence this problem. Now if I had a time machine...

(When I Google I get lots of hits, however they are mostly sites that won't let you see the answer without having to register on the site first.)

like image 727
Ian Ringrose Avatar asked May 15 '09 10:05

Ian Ringrose


People also ask

How do I associate a user to login in SQL Server?

Use sp_change_users_login to link a database user in the current database with a SQL Server login. If the login for a user has changed, use sp_change_users_login to link the user to the new login without losing user permissions.

How do I fix orphaned Users in SQL Server after Restore?

To fix any orphaned users, use create login by using SID. USING UPDATE_ONE : UPDATE_ONE could be used to map even when Login name and User name are different or could be used to change user's SID with Logins SID.

How do I give permission to all Users in SQL Server?

Login to SQL Server Management Studio. In Object Explorer on the left pane, expand the Databases folder and select the concerned database and navigate to the by expanding Security and Users folders. Right-click the User to which you want to GRANT or REVOKE the permissions.


1 Answers

Yes, you can do that by executing:

EXEC sp_change_users_login 'Auto_Fix' , 'TheUserName'; 

However if your question was can I fix all users automatically then this won't do that.

like image 198
PQW Avatar answered Oct 14 '22 08:10

PQW