SSIS 中的 UPSERT
我正在编写一个在 SQL Server 2008 上运行的 SSIS 包。如何在 SSIS 中执行 UPSERT?
IF KEY NOT EXISTS INSERT ELSE IF DATA CHANGED UPDATE ENDIF ENDIF
I am writing an SSIS package to run on SQL Server 2008. How do you do an UPSERT in SSIS?
IF KEY NOT EXISTS INSERT ELSE IF DATA CHANGED UPDATE ENDIF ENDIF
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除了基于 T-SQL 的解决方案(这甚至没有标记为 sql/tsql),您可以将 SSIS 数据流任务与合并连接结合使用,如下所述 此处(以及 其他地方)。
关键部分是排序源的合并连接中的完全外连接(如果您只想插入/更新而不删除左外连接)。
后面跟着一个条件分割来知道接下来要做什么:插入到目标(这也是我的源),更新它(通过 SQL 命令),或者从中删除(再次通过 SQL 命令) 。
Apart from T-SQL based solutions (and this is not even tagged as sql/tsql), you can use an SSIS Data Flow Task with a Merge Join as described here (and elsewhere).
The crucial part is the Full Outer Join in the Merger Join (if you only want to insert/update and not delete a Left Outer Join works as well) of your sorted sources.
followed by a Conditional Split to know what to do next: Insert into the destination (which is also my source here), update it (via SQL Command), or delete from it (again via SQL Command).
在 sql 中创建 upsert 的另一种方法(如果您有预阶段表或阶段表):
Another way to create an upsert in sql (if you have pre-stage or stage tables):
多年来一直使用的基本数据操作语言 (DML) 命令是更新、插入和删除。 它们完全符合您的预期:插入添加新记录,更新修改现有记录,删除删除记录。
UPSERT 语句修改现有记录,如果记录不存在,则插入新记录。
UPSERT 语句的功能可以通过两组新的 TSQL 运算符来实现。 这是两个新值
Except:-
返回 EXCEPT 操作数左侧查询中未从右侧查询返回的任何不同值
Intersect:-
返回 INTERSECT 操作数左侧和右侧的查询返回的任何不同值。
示例:- 假设我们有两个表,表 1 和表 2
将返回 3,4,因为它出现在 Table_1 中,而不出现在 Table_2 中,
将返回 1,2,5,因为它们出现在表 Table_1 和 Table_2 中。
现在,复杂连接的所有麻烦都已消除:-)
要在 SSIS 中使用此功能,您只需添加一个“执行 SQL”任务并将代码放入其中即可。
The basic Data Manipulation Language (DML) commands that have been in use over the years are Update, Insert and Delete. They do exactly what you expect: Insert adds new records, Update modifies existing records and Delete removes records.
UPSERT statement modifies existing records, if a records is not present it INSERTS new records.
The functionality of UPSERT statment can be acheived by two new set of TSQL operators. These are the two new ones
Except:-
Returns any distinct values from the query to the left of the EXCEPT operand that are not also returned from the right query
Intersect:-
Returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand.
Example:- Lets say we have two tables Table 1 and Table 2
will return 3,4 as it is present in Table_1 not in Table_2
will return 1,2,5 as they are present in both tables Table_1 and Table_2.
All the pains of Complex joins are now eliminated :-)
To use this functionality in SSIS, all you need to do add an "Execute SQL" task and put the code in there.
我通常更喜欢让 SSIS 引擎来管理增量合并。 仅插入新项目并更新更改。
如果您的目标服务器没有足够的资源来管理繁重的查询,此方法允许使用 SSIS 服务器的资源。
I usually prefer to let SSIS engine to manage delta merge. Only new items are inserted and changed are updated.
If your destination Server does not have enough resources to manage heavy query, this method allow to use resources of your SSIS server.
我们可以使用SSIS中缓慢变化的维度组件来进行更新插入。
使用指南。
We can use slowly changing dimension component in SSIS to upsert.
Guide on usage.
我会使用“缓慢变化的维度”任务
I would use the 'slow changing dimension' task