1
votes

I am trying to do an UPSERT in DB2 9.7 without creating a temporary table to merge. I am specifying values as parameters, however I'm always getting a syntax error for the comma separating the values when I try to include more than one row of values.

MERGE INTO table_name AS tab 
USING (VALUES 
        (?,?), 
        (?,?)
    ) AS merge (COL1, COL2) 
    ON tab.COL1 = merge.COL1 
    WHEN MATCHED THEN 
        UPDATE SET tab.COL1 = merge.COL1, 
                   tab.COL2 = merge.COL2
    WHEN NOT MATCHED THEN 
        INSERT (COL1, COL2) 
        VALUES (merge.COL1, merge.COL2)

I have also tried teknopaul's answer from Does DB2 have an “insert or update” statement, but have received another syntax error complaining about the use of SELECT.

Does anybody know how to correctly include a table with values in my merge, without actually creating/dropping one on the database?

1
Are you always filling out all rows? Or only a subset?Clockwork-Muse
@Clockwork-Muse Yes all rows. I want it to update records if they are already there, and insert them if they are not.Crobota
Don't use a reserved word for the correlation name: AS merge (COL1, COL2).mustaccio
@mustaccio Good point...however I'm still receiving the same error: ERROR [42601] [IBM][CLI Driver][DB2] SQL0104N An unexpected token "," was found following "". Expected tokens may include: "FOR )". SQLSTATE=42601Crobota

1 Answers

-2
votes

I believe you need something like USING (SELECT * FROM VALUES ( ...) ) AS ...