简体   繁体   中英

How to transfer a database AND its users from one SQL Server to another?

we have one db in an SQLServer instance that should be transferred to another instance (let's say dbname="testdb" with a user testuser ).

This transfer works easily when we use SQL Server Management Studio (backup the database on the source machine and restore it on the target machine).

The problem is now that I can't connect with the testuser on the target machine. It's part of the db but not part of the SQL Server wide logins.

Now my question. How can I add the user from the db to the SQL Server logins?

Thanks in advance for any comments!

Cheers, Helmut

What really helped me in the end was running the following SQL script (after restore)

USE testdb;
GO
EXEC sp_change_users_login 'Auto_Fix', 'testuser', NULL, 'testpwd';
GO

This "connects" the database user "testuser" to the server login "testuser". If no server login "testuser" exists a new one is created.

For documentation please see http://msdn.microsoft.com/en-us/library/ms174378.aspx

I used to face the same problem dear. The Solution for his is You have to create login in server with the Statement below

Use [testdb] --Your Database name

CREATE USER [testuser] FOR LOGIN [testuser]

here your [testuser] is your database user and the second [testuser] your server login name and with this statement we mapped each other.

I recommend to transfer the logins and passwords between your instances of SQL Server.
There is a Microsoft Documentation in this link https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/security/transfer-logins-passwords-between-instances

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM