简体   繁体   English

如何在SQL Server中将数据从一个表复制到另一个表

[英]How to copy data from one table to another in SQL Server

I am trying to copy data from views on a trusted SQL Server 2012 to tables on a local instance of SQL Server on a scheduled transfer . 我正在尝试按计划的传输将数据从受信任的SQL Server 2012上的视图复制到SQL Server本地实例上的 What would be the best practice for this situation? 在这种情况下,最佳做法是什么?

Here are the options I have come up with so far: 到目前为止,我提供了以下选项:

  1. Write an executable program in C# or VB to delete existing local table, query the data from remote database and then write results to tables in the local database. 用C#或VB编写可执行程序以删除现有的本地表,从远程数据库中查询数据,然后将结果写入本地数据库中的表。 The executable would run on a scheduled task. 该可执行文件将在计划任务上运行。

  2. Use BCP to copy data to a file and then upload into local table. 使用BCP将数据复制到文件,然后上传到本地表。

  3. Use SSIS 使用SSIS

Note: The connection between local and remote SQL Server is very slow. 注意:本地和远程SQL Server之间的连接非常慢。

Since the transfers are scheduled, so I suppose you want this data to be up-to-date. 由于传输是有计划的,因此我想您希望此数据是最新的。

My recommendation would be to use SSIS and schedule it using SQL Agent. 我的建议是使用SSIS并使用SQL Agent计划它。 If you wrote a C# program, I think the best outcome you will gain is a program imitating SSIS. 如果您编写了C#程序,我认为您将获得的最佳结果是模仿SSIS的程序。 Moreover, SSIS will be a very easy to amend the workflow anytime. 而且,SSIS随时随地都是很容易修改的工作流程。

Either way, to make such program/package up-to-date, you will have to answer an important question: Is the source table updatable or is it like a log (inserts only)? 无论哪种方式,要使此类程序/程序包都是最新的,您都必须回答一个重要的问题: 源表是可更新的还是像日志(仅插入)? This question is so important because it will determine how you will fetch the new updates from the source table. 这个问题是如此重要,因为它将决定您如何从源表中获取新更新。 For example, if the table represents logs, you will most probably use the Primary Key to detect new records, if not, you might want to seek a column representing update date/time. 例如,如果表代表日志,则您很可能会使用主键来检测新记录,如果不是,则可能要查找代表更新日期/时间的列。 If you have the authority to alter the source table, you might want to add timestamp column which represent the row version ( timestamp differs than datetime ) 如果您有权更改源表,则可能需要添加代表行版本的timestamp列( timestampdatetime不同)

For building an SSIS package, it will mainly contain the following components: 对于构建SSIS包,它将主要包含以下组件:

  1. Execute SQL Task to get the maximum value from source table. Execute SQL Task以从源表获取最大值。

  2. Execute SQL Task to get the last value where it should start from at the destination table. Execute SQL Task以获取应从目标表开始的最后一个值。 You can get this value either by selecting the maximum value from the destination table or if the table is pretty large you can store that value in another table (configuration table for example). 您可以通过从目标表中选择最大值来获得该值,或者如果该表很大,则可以将该值存储在另一个表(例如配置表)中。

  3. Data Flow which moves the data from source table starting after the value fetched in step 2 to the value fetched in step 1. Data Flow ,该数据Data Flow将数据从源表中移至步骤2中获取的值之后,将其从步骤2中获取的值开始。

  4. Execute SQL Task for updating the new maximum value back to the configuration table if you chose this technique. 如果选择了此技术,请Execute SQL Task以将新的最大值更新回配置表。

BCP can be used to export the data compress and transfer over network which can be then imported into local instance of SQL. BCP可以用于导出数据压缩和通过网络传输,然后可以将其导入到SQL的本地实例中。 Also with BCP data exports can be contained with smaller batches of data for easier management of data. 同样,对于BCP,数据导出可以包含在较小批量的数据中,以便于数据管理。

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

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