1

朋友们,我正在使用合并语法来更新和插入表中的值

MERGE INTO iq_dnr b
   USING (SELECT *
            FROM iq_dnr
           WHERE co_series = 'IO' AND CATEGORY = 9 AND dnr_from = 13567362) a
   ON (    a.co_series = b.co_series
       AND a.CATEGORY = b.CATEGORY
       AND a.dnr_from = b.dnr_from)
   WHEN MATCHED THEN
      UPDATE
         SET b.dnr_to = 0001
   WHEN NOT MATCHED THEN
      INSERT (co_series, CATEGORY, dnr_from, dnr_to)
      VALUES ('IO', 9, 13567362, 00001)

我想问是否可以像我在上面的查询中那样只检查和插入一个表。知道问题是我能够成功更新表,但我无法插入值。请帮助我

4

1 回答 1

3

尝试使用 DUAL,它可能会解决您的问题:

MERGE INTO iq_dnr b
   USING (SELECT 'IO' co_series,
                  9 CATEGORY,
                  13567362 dnr_from
            FROM dual) a
   ON (    a.co_series = b.co_series
       AND a.CATEGORY = b.CATEGORY
       AND a.dnr_from = b.dnr_from)
   WHEN MATCHED THEN
      UPDATE
         SET b.dnr_to = 0001
   WHEN NOT MATCHED THEN
      INSERT (co_series, CATEGORY, dnr_from, dnr_to)
      VALUES ('IO', 9, 13567362, 00001)

使用上述方法DUAL确保它无论如何都会返回一些东西,从而使 MERGE 语句保持工作......

于 2012-08-21T04:59:55.903 回答