【发布时间】:2018-02-07 19:30:55
【问题描述】:
我在使用 WITH 子句将 Oracle Merge 迁移到 Postgres 时遇到问题。
以下是 ORACLE 的示例代码:
SELECT * FROM source;
ID STATUS DESCRIPTION
---------- ---------- -----------------------
1 20 Description of level 1
2 10 Description of level 2
3 20 Description of level 3
4 10 Description of level 4
5 20 Description of level 5
目的地表如下:
SELECT * FROM destination;
1 20 Description of level 1
2 10 Description of level 2
3 20 Description of level 3
4 10 Description of level 4
5 20 Description of level 5
6 10 Description of level 6
7 20 Description of level 7
8 10 Description of level 8
9 20 Description of level 9
10 10 Description of level 10
我在 ORACLE 中有合并实现,如下所示:合并时:
MERGE INTO destination d
USING source s
ON (s.id = d.id)
WHEN MATCHED THEN
UPDATE SET d.description = 'Updated'
DELETE WHERE d.status = 10;
5 rows merged.
SELECT * FROM destination;
ID STATUS DESCRIPTION
---------- ---------- -----------------------
1 20 Updated
3 20 Updated
5 20 Updated
6 10 Description of level 6
7 20 Description of level 7
8 10 Description of level 8
9 20 Description of level 9
10 10 Description of level 10
8 rows selected.
我能够使用 CTE 转换在 MATCHED 和 UNMATCHED 子句中都有一个条件的 Merge。 但如果我在同一个子句中有两个条件,不知道该怎么做(删除和更新)。
编辑: 我理解 a_horse_with_no_name 分享的答案。但是在以下情况下我是否需要进行嵌套 CTE 感到困惑。
MERGE INTO destination d
USING source s
ON (s.id = d.id)
WHEN MATCHED THEN
UPDATE SET d.description = 'Updated'
DELETE WHERE d.status = 10
WHEN NOT MATCHED THEN
INSERT (ID, STATUS, DESCRIPTION) VALUES (s.id,s.status,s.description);
如果我的合并语句也没有匹配,那么我该怎么做。
【问题讨论】:
标签: sql postgresql common-table-expression postgresql-9.4