SSIS 中的 UPSERT

发布于 2024-07-27 18:34:03 字数 152 浏览 6 评论 0原文

我正在编写一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

余厌 2024-08-03 18:34:03

除了基于 T-SQL 的解决方案(这甚至没有标记为 /),您可以将 SSIS 数据流任务与合并连接结合使用,如下所述 此处(以及 其他地方)。

输入图像描述这里

关键部分是排序源的合并连接中的完全外连接(如果您只想插入/更新而不删除左外连接)。

输入图像描述这里

后面跟着一个条件分割来知道接下来要做什么:插入到目标(这也是我的源),更新它(通过 SQL 命令),或者从中删除(再次通过 SQL 命令) 。

  1. 插入:如果仅在源中找到 gid(左)
  2. 更新 如果源和目标上都存在 gid
  3. 删除:如果在源中未找到 gid,但在目标中存在(右)

在此处输入图像描述

Apart from T-SQL based solutions (and this is not even tagged as /), you can use an SSIS Data Flow Task with a Merge Join as described here (and elsewhere).

enter image description here

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.

enter image description here

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).

  1. INSERT: If the gid is found only on the source (left)
  2. UPDATE If the gid exists on both the source and destination
  3. DELETE: If the gid is not found in the source but exists in the destination (right)

enter image description here

别在捏我脸啦 2024-08-03 18:34:03

在 sql 中创建 upsert 的另一种方法(如果您有预阶段表或阶段表):

--Insert Portion
INSERT INTO FinalTable
( Colums )
SELECT T.TempColumns
FROM TempTable T
WHERE
(
    SELECT 'Bam'
    FROM FinalTable F
    WHERE F.Key(s) = T.Key(s)
) IS NULL

--Update Portion
UPDATE FinalTable
SET NonKeyColumn(s) = T.TempNonKeyColumn(s)
FROM TempTable T
WHERE FinalTable.Key(s) = T.Key(s)
    AND CHECKSUM(FinalTable.NonKeyColumn(s)) <> CHECKSUM(T.NonKeyColumn(s))

Another way to create an upsert in sql (if you have pre-stage or stage tables):

--Insert Portion
INSERT INTO FinalTable
( Colums )
SELECT T.TempColumns
FROM TempTable T
WHERE
(
    SELECT 'Bam'
    FROM FinalTable F
    WHERE F.Key(s) = T.Key(s)
) IS NULL

--Update Portion
UPDATE FinalTable
SET NonKeyColumn(s) = T.TempNonKeyColumn(s)
FROM TempTable T
WHERE FinalTable.Key(s) = T.Key(s)
    AND CHECKSUM(FinalTable.NonKeyColumn(s)) <> CHECKSUM(T.NonKeyColumn(s))
南城旧梦 2024-08-03 18:34:03

多年来一直使用的基本数据操作语言 (DML) 命令是更新、插入和删除。 它们完全符合您的预期:插入添加新记录,更新修改现有记录,删除删除记录。

UPSERT 语句修改现有记录,如果记录不存在,则插入新记录。
UPSERT 语句的功能可以通过两组新的 TSQL 运算符来实现。 这是两个新值

EXCEPT
INTERSECT

Except:-

返回 EXCEPT 操作数左侧查询中未从右侧查询返回的任何不同值

Intersect:-
返回 INTERSECT 操作数左侧和右侧的查询返回的任何不同值。

示例:- 假设我们有两个表,表 1 和表 2

Table_1 column name(Number, datatype int)
----------

1
2

3
4
5

Table_2 column name(Number, datatype int)
----------

1
2

5

SELECT * FROM TABLE_1 EXCEPT SELECT * FROM  TABLE_2 

将返回 3,4,因为它出现在 Table_1 中,而不出现在 Table_2 中,

SELECT * FROM TABLE_1 INTERSECT SELECT * FROM  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
INTERSECT

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

Table_1 column name(Number, datatype int)
----------

1
2

3
4
5

Table_2 column name(Number, datatype int)
----------

1
2

5

SELECT * FROM TABLE_1 EXCEPT SELECT * FROM  TABLE_2 

will return 3,4 as it is present in Table_1 not in Table_2

SELECT * FROM TABLE_1 INTERSECT SELECT * FROM  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.

虚拟世界 2024-08-03 18:34:03

我通常更喜欢让 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.

鸠魁 2024-08-03 18:34:03

我们可以使用SSIS中缓慢变化的维度组件来进行更新插入。

使用指南

We can use slowly changing dimension component in SSIS to upsert.

Guide on usage.

等风来 2024-08-03 18:34:03

我会使用“缓慢变化的维度”任务

I would use the 'slow changing dimension' task

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文