我正在尝试编写一个查询逻辑来从源捕获所有更改。
Merge应该比较源和目标中的键,并相应地处理数据。下面是一个例子,我们如何在SQL server中实现这一点。
MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition>
[WHEN MATCHED
THEN <merge_matched> ]
[WHEN NOT MATCHED [BY TARGET]
THEN <merge_not_matched> ]
[WHEN NOT MATCHED BY SOURCE
THEN <merge_matched> ];
在snowflake中,我在尝试类似的东西时得到以下错误,SQL编译错误:语法错误第7行第17位意外'by'。语法错误,第8行第17位意外'by'.
是否有一种方法在雪花处理'当不匹配的目标'和'当不匹配的源'逻辑?
谢谢。
队友建议的变通方法:
- 基于全连接定义
MATCHED_BY_SOURCE
,并查看a.col或b.col是否为null:
merge into TARGET t
using (
select <COLUMN_LIST>,
iff(a.COL is null, 'NOT_MATCHED_BY_SOURCE', 'MATCHED_BY_SOURCE') SOURCE_MATCH,
iff(b.COL is null, 'NOT_MATCHED_BY_TARGET', 'MATCHED_BY_TARGET') TARGET_MATCH
from SOURCE a
full join TARGET b
on a.COL = b.COL
) s
on s.COL = t.COL
when matched and s.SOURCE_MATCH = 'NOT_MATCHED_BY_SOURCE' then
<DO_SOMETHING>
when matched and s.TARGET_MATCH = 'NOT_MATCHED_BY_TARGET' then
<DO_SOMETHING_ELSE>
;