MERGE for Oracle SQL中的多个子句:在单个语句中使用UPSERT AND DELETE

时间:2017-01-04 07:01:17

标签: java sql oracle

我的查询形式如下:

public static final String CLASS_UPSERT_QUERY = Joiner.on(" ").join(
    "MERGE INTO CLASS USING DUAL ON",
    "(<condition1>)",
    "WHEN MATCHED THEN",
    "UPDATE SET ()",
    "WHEN NOT MATCHED THEN",
    "INSERT () VALUES ()"
);

我怀疑是否可能在同一声明中有多个条件。我需要根据某些条件进行upsert并删除另一个条目。

下面的内容:

public static final String CLASS_UPSERT_QUERY = Joiner.on(" ").join(
    "MERGE INTO CLASS USING DUAL ON",
    "(<condition1>)",
    "WHEN MATCHED THEN",
    "UPDATE SET ()",
    "WHEN NOT MATCHED THEN",
    "(",
    "MERGE INTO CLASS USING DUAL ON(<condition2>)",
    "WHEN MATCHED THEN",
    "DELETE ()",
    "WHEN NOT MATCHED THEN",
    "INSERT () VALUES ()",
    ")"
);

2 个答案:

答案 0 :(得分:0)

如果从问题中取出java部分,这看起来像Oracle中的简单Merge语句,您可以根据条件插入或更新,如下所示:

MERGE INTO tgt_tbl a
  USING src_tbl b
    ON (a.id = b.id)
  WHEN MATCHED THEN
    UPDATE SET a.status = b.status
    WHERE  b.status != 'VALID'
  WHEN NOT MATCHED THEN
    INSERT (id, status)
    VALUES (b.id, b.status)
    WHERE  b.status != 'VALID';

答案 1 :(得分:0)

我将根据oracle的观点回答。

何时使用合并声明:从一个或多个源中选择行以进行更新或插入表或视图。或者在单个语句中组合插入/更新/删除。

语法:

arr2.map...

请提供您的表格和示例数据,以便您更好地了解问题。您尚未向我们提供任何样本数据或表格。