简体   繁体   中英

Sql Server copy data from one database table to another

I am making some some data changes to a dev database, and hope to copy the changes over to the live database when I am done. There may be some new rows, although mostly there will be updated rows. Ie rows that exist in the live db already.

There are several tables in the database and I only want to copy data from a few of them.

If possible, I am hoping that this is be possible without having to write any script, and by making use of one of the tools that comes with Management Studio .

I tried using the Generate and Publish Scripts wizard in Management Studio , and created a script that copied all the data from the tables that I wish to copy. The problem here is, the script attempts to insert rows with Ids that already exist in the case of updated rows. So only the new rows are inserted.

I want to be able to automatically generate a script that will update the row if it already exists, or just insert a new row if it doesn't.

Is there some other tool, or option using the Generate and Publish Scripts wizard that will allow me to do this?

I know its late to answer now but sharing for others who are still facing same problem.

I was also having same scenario in one of my project in recent, I accomplished it by writing one stored procedure (Ref : Copy changed data in table from source to destination in SQL ) which will create "MERGE" statement for each table to copy the differences from source database to target database for particular table.

To know more about MERGE statement you can visit this MSDN link

There is a program called SQL Compare that will help me achieve what I wanted to do, without having to write any script myself. Quite pricey but a trial version is available.

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