简体   繁体   English

SQL Server将数据从一个数据库表复制到另一数据库表

[英]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 . 如果可能的话,我希望无需编写任何脚本,并使用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. 我尝试使用Management Studio的“ Generate and Publish Scripts向导,并创建了一个脚本,该脚本从希望复制的表中复制了所有数据。 The problem here is, the script attempts to insert rows with Ids that already exist in the case of updated rows. 这里的问题是,脚本尝试使用更新的行插入具有ID的行。 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? 是否有其他工具或使用“ Generate and Publish Scripts向导的选项允许我执行此操作?

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. 最近我在一个项目中也遇到了同样的情况,我通过编写一个存储过程(参考: 在SQL中将表中的更改数据从源复制到目标 )来完成该操作,该存储过程将为每个表创建“ MERGE”语句以复制特定表在源数据库和目标数据库之间的差异。

To know more about MERGE statement you can visit this MSDN link 要了解有关MERGE语句的更多信息,请访问此MSDN 链接。

There is a program called SQL Compare that will help me achieve what I wanted to do, without having to write any script myself. 有一个名为SQL Compare的程序,可以帮助我实现自己想做的事情,而不必自己编写任何脚本。 Quite pricey but a trial version is available. 价格很高,但有试用版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将特定表数据从一个数据库复制到SQL Server中的另一个数据库 - Copy specific table data from one database to another in SQL Server 我可以将数据从一个数据库表复制到另一个已经存在的数据库表sql服务器吗? - Can i copy data from one database table to another already existing database table sql server? 如何使用SQL Server将表和数据从一个数据库复制到另一个数据库 - How to copy table and data from one database to another database using SQL Server 将表数据从一个数据库复制到另一个数据库SQL - Copy a table data from one database to another database SQL SQL Server:将列数据值从一个表复制到同一数据库中的另一表 - SQL Server: copy column data values from one table to another table in the same database 在 SQL 服务器中将 Partial Table 从一个数据库复制到另一个数据库 - Copy Partial Table from one database to another in SQL Server 使用SQL Server将数据从一个表复制到另一表 - Copy data from one table to another table using sql server 将数据从一个表复制到SQL Server中的另一个表 - Copy data from one table to another in sql server 将所选数据从一个表复制到SQL Server中的另一个表 - Copy selected data from one table to another in SQL Server 在同一个表中的 SQL Server 中将数据从一行复制到另一行 - Copy data from one row to another in SQL Server in the same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM