将 MS SQL 的 IF-ELSE 语句翻译成 SnowFlake
Translate IF-ELSE statement of MS SQL to SnowFlake
我有一个 MS SQL 查询是这样的:
IF EXISTS(SELECT * FROM TableDescription WHERE "viewname"='abc')
UPDATE TableDescription
SET "viewname"='abc' , "description"='description xyz'
WHERE "viewname"='abc'
ELSE
INSERT INTO TableDescription ("viewname","description") VALUES('abc','description xyz')
我想将上面的 MS SQL 查询翻译成适用于 SnowFlake 的对应查询,但不确定我该怎么做:(
有人可以给我提示吗?
非常感谢
韩
MERGE 可以使用:
MERGE TableDescription t
USING (SELECT 'abc' AS "viewname", 'description xyz' AS "description")s
ON t."viewname" = s."viewname"
WHEN MATCHED THEN
UPDATE SET t."viewname" = s."viename",
t."description"= s."description
WHEN NOT MATCHED THEN
INSERT("viewname", "description")
VALUES (s."viewname", s."description");
我有一个 MS SQL 查询是这样的:
IF EXISTS(SELECT * FROM TableDescription WHERE "viewname"='abc')
UPDATE TableDescription
SET "viewname"='abc' , "description"='description xyz'
WHERE "viewname"='abc'
ELSE
INSERT INTO TableDescription ("viewname","description") VALUES('abc','description xyz')
我想将上面的 MS SQL 查询翻译成适用于 SnowFlake 的对应查询,但不确定我该怎么做:( 有人可以给我提示吗? 非常感谢 韩
MERGE 可以使用:
MERGE TableDescription t
USING (SELECT 'abc' AS "viewname", 'description xyz' AS "description")s
ON t."viewname" = s."viewname"
WHEN MATCHED THEN
UPDATE SET t."viewname" = s."viename",
t."description"= s."description
WHEN NOT MATCHED THEN
INSERT("viewname", "description")
VALUES (s."viewname", s."description");