如何 INSERT 不重复,我可以将 MERGE 与自引用一起使用吗?
How to INSERT non-duplicated, can I use MERGE with self-reference?
我需要插入非重复值...此 "merge with self-reference" 未按预期工作,但未生成错误消息...我可以使用类似的东西吗?
MERGE INTO mydb.mytab AS Tref
USING mydb.mytab AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (123, 'etc');
(更新但不改变问题)似乎其他构造,如建议 here,是
INTO INTO mydb.mytab
SELECT t1.*
FROM ( select stack(2, 123,'ect1', 20,'etc2') as (id,etc) ) t1
LEFT OUTER JOIN mydb.mytab t2 ON t1.id = t2.id
WHERE t1.id is null
;
它不优雅(还有其他的?)但它正在工作(!)。 MERGE 有一些替代方案吗?
仅当 mydb.mytab 不包含匹配行
时才会插入合并
MERGE INTO mydb.mytab AS Tref
USING ( select stack(2, 123,'ect1', 20,'etc2') as (id,etc) ) AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (t.id, t.etc) --columns positions should match those in Tref
;
我需要插入非重复值...此 "merge with self-reference" 未按预期工作,但未生成错误消息...我可以使用类似的东西吗?
MERGE INTO mydb.mytab AS Tref
USING mydb.mytab AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (123, 'etc');
(更新但不改变问题)似乎其他构造,如建议 here,是
INTO INTO mydb.mytab
SELECT t1.*
FROM ( select stack(2, 123,'ect1', 20,'etc2') as (id,etc) ) t1
LEFT OUTER JOIN mydb.mytab t2 ON t1.id = t2.id
WHERE t1.id is null
;
它不优雅(还有其他的?)但它正在工作(!)。 MERGE 有一些替代方案吗?
仅当 mydb.mytab 不包含匹配行
时才会插入合并MERGE INTO mydb.mytab AS Tref
USING ( select stack(2, 123,'ect1', 20,'etc2') as (id,etc) ) AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (t.id, t.etc) --columns positions should match those in Tref
;