2

我想要做的是将不同版本插入/更新到产品表中。这是一个 PL/SQL 块,我有一个保存版本号的变量。假设版本是 10。现在,我想使用合并语句为同一产品插入或更新版本 11 - 15。所以,我需要检查表中现有的产品和版本(我有 product_id)。我需要生成数字 11 到 15 并将其与 product_id 一起对照表进行检查。所以,我的问题是在 using 子句中 - 我需要使用包含 10 的临时变量生成版本号(11-15),并作为我的第二列(版本)进行补充。请根据我的要求更新我在下面的基本 MERGE 语句。谢谢。

MERGE INTO product a
USING (SELECT product_id,
              version_id/variable
         FROM product
        WHERE product_id = 1234
      ) b 
      ON (a.product_id = b.product_id AND a.version_id = b.version_id)
 WHEN MATCHED THEN
    [UPDATE product]
 WHEN NOT MATCHED THEN
    [INSERT INTO product]
4

2 回答 2

9

选择的答案有点复杂,所以这里是我合并到同一张表中的答案

MERGE INTO YOUR_TABLE
USING DUAL
    ON ( USER_ID = '123' AND USER_NAME= 'itszaif') 
WHEN NOT MATCHED THEN
        INSERT ( USERS_ID, USER_NAME)
        VALUES ('123','itszaif');

该命令检查是否匹配USER_IDUSER_NAME如果不匹配则插入。

于 2017-05-16T15:03:43.807 回答
4

You can use a row generator, e.g. this will generate version IDs 11 to 15 for your query, so that your merge can insert/update them as required:

MERGE INTO product a
USING (SELECT product.product_id,
              q.version_id
         FROM product
             ,(SELECT ROWNUM+10 version_id FROM dual CONNECT BY LEVEL <= 5) q
        WHERE product_id = 1234
      ) b 
ON (a.product_id = b.product_id AND a.version_id = b.version_id)
WHEN MATCHED THEN
  [UPDATE product]
WHEN NOT MATCHED THEN
  [INSERT INTO product]
于 2013-06-14T03:20:27.640 回答