basic MERGE into same table

Curtis picture Curtis · Jun 13, 2013 · Viewed 25.1k times · Source

What I am trying to do is insert/update different versions into a product table. This is a PL/SQL chunk and i have a variable that holds the version number. Lets say the version is 10. Now, I wanna use the merge statement to insert or update versions 11 - 15 for the same product. So, I need to check the table for existing product & version(I have product_id). I need to generate numbers 11 through 15 and check it against the table along with product_id. So, my question is in the using clause - i need to generate the version numbers(11-15) using my temp variable that holds 10 and supplement as my second column(version). Please update the base MERGE statement i have below for my requirement. Thanks.

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]

Answer

Zafrullah Syed picture Zafrullah Syed · May 16, 2017

The selected answer is a bit complex, so here is my answer to merge into the same table

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');

This command checks if USER_ID and USER_NAME are matched, if not matched then it will insert.