[英]Merge table, track non-matches
I'm updating table A with table B (a temp table build from a csv file).我正在用表 B 更新表 A(从 csv 文件构建的临时表)。 B might contain records not found in A, if that's the case then I need to know.
B 可能包含在 A 中找不到的记录,如果是这种情况,那么我需要知道。 So either I need the record from B deleted after A is updated, or output B record when no match is found in A. I've never used MERGE before and just can't seem to get the syntax correct.
因此,要么我需要在 A 更新后删除 B 中的记录,要么在 A 中找不到匹配项时需要 output B 记录。我以前从未使用过 MERGE,只是似乎无法获得正确的语法。
MERGE INTO table_A
USING table_B
ON table_A.id = table_B.id
WHEN MATCHED AND table_A.account = 2 THEN
UPDATE SET
tableA.col2 = tableB.col2,
tableA.col3 = tableB.col3
DELETE;
SELECT *
FROM table_B
Not sure if this is the best approach, but I got the results I wanted.不确定这是否是最好的方法,但我得到了我想要的结果。
CREATE TABLE tableC ( )
INSERT INTO tableC(col1, col2, col3)
SELECT col1, col2, col3
FROM (
MERGE INTO table_A
USING table_B
ON table_A.id = table_B.id
WHEN MATCHED AND table_A.account = 2 THEN
UPDATE SET
tableA.col2 = tableB.col2,
tableA.col3 = tableB.col3
OUTPUT tableC.*
) myoutput
SELECT *
FROM tableB
WHERE col1 NOT IN (
SELECT col1
FROM tableC
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.