如何跨多个数据库服务器使用 MERGE 语句? [英] How can I use MERGE statement across multiple database servers?

查看:38
本文介绍了如何跨多个数据库服务器使用 MERGE 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的源表和目标表存在于不同的服务器上.我正在使用 Execute SQL Task 编写合并语句来同步它们.

My Source and Destination tables exist on different servers. I am using Execute SQL Task to write Merge Statements to synchronize them.

谁能解释我如何在我的执行 SQL 任务中引用存在于不同服务器上的两个不同数据库?

Could anyone explain how I can reference two different databases that exist on different servers inside my Execute SQL Task?

推荐答案

可能的方法:

我建议使用以下方法,而不是尝试在两个之间的 Execute SQL Task 中使用 MERGE 语句数据库服务器.

Possible approaches:

I would suggest the following approaches instead of trying to use MERGE statement within Execute SQL Task between two database servers.

  • 为每个 SQL Server 实例创建两个 OLEDB 连接管理器.例如,如果您有两个数据库 SourceDB 和 DestinationDB,您可以创建两个名为 OLEDB_SourceDBOLEDB_DestinationDB 的连接管理器.如果您愿意,也可以使用 ADO.NET 连接管理器.根据我在基于 SSIS 的书籍中阅读的内容,OLEDB 的性能优于 ADO.NET 连接管理器.

  • Create two OLEDB Connection Managers to each of the SQL Server instances. For example, if you have two databases SourceDB and DestinationDB, you could create two connection managers named OLEDB_SourceDB and OLEDB_DestinationDB. You could also use ADO.NET connection manager, if you prefer that. Based on what I have read in SSIS based books, OLEDB performs better than ADO.NET connection manager.

Data Flow Task 拖放到 Control Flow 选项卡上.

Drag and drop a Data Flow Task on the Control Flow tab.

数据流任务中,配置一个OLE DB Source以从源数据库表中读取数据.

Within the Data Flow Task, configure an OLE DB Source to read the data from source database table.

使用 Lookup Transformation 使用源表和目标表之间的唯一键检查数据是否已存在于目标表中.

Use Lookup Transformation that checks whether if the data already exists in the destination table using the uniquer key between source and destination tables.

如果源表行在目标表中不存在,则使用OLE DB Destination

如果源表行 exists 在目标表中,则使用另一个 OLE DB 将这些行插入到目标数据库上的临时表中目的地.

控制流选项卡上的数据流任务之后放置一个执行SQL任务.编写一个查询,使用临时表数据更新目标表中的数据.

Place an Execute SQL Task after the Data Flow Task on the Control Flow tab. Write a query that would update the data in destination table using the staging table data.

查看以下 SO 问题的答案以了解详细步骤.

Check the answer to the below SO question for detailed steps.

怎么做我在 SSIS 包中优化 Upsert(更新和插入)操作?

  • 为每个 SQL Server 实例创建两个 OLEDB 连接管理器.例如,如果您有两个数据库 SourceDB 和 DestinationDB,您可以创建两个名为 OLEDB_SourceDBOLEDB_DestinationDB 的连接管理器.

  • Create two OLEDB Connection Managers to each of the SQL Server instances. For example, if you have two databases SourceDB and DestinationDB, you could create two connection managers named OLEDB_SourceDB and OLEDB_DestinationDB.

Data Flow Task 拖放到 Control Flow 选项卡上.

Drag and drop a Data Flow Task on the Control Flow tab.

数据流任务中,配置一个OLE DB Source从源数据库表中读取数据并插入到使用 OLE DB Destination 的临时表.

Within the Data Flow Task, configure an OLE DB Source to read the data from source database table and insert into a staging table using OLE DB Destination.

Data Flow Task 之后放置一个 Execute SQL TaskControl流标签.编写一个查询,在临时表和目标表之间使用 MERGE 语句.

Place an Execute SQL Task after the Data Flow Task on the Control Flow tab. Write a query that would use the MERGE statement between staging table and the destination table.

这篇关于如何跨多个数据库服务器使用 MERGE 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆