简体   繁体   中英

Not able to merge in DB2

I'm trying to do UPSERT(Update or insert) in DB2 AS400. I've followed the guidelines which are specified by IBM. But, it says as MERGE is not valid. Here is my query.

MERGE INTO myTable AS tab
USING (VALUES
    (123pk,'6','11','22','33',CURDATE())
) AS merge (TSPK,adf1,adf2,adf3,adf4,adf5)
ON tab.TSPK= merge.TSPK
WHEN MATCHED THEN
    UPDATE SET tab.TSPK= merge.TSPK,
               tab.adf1= merge.adf1,
               tab.adf2= merge.adf2,
               tab.adf3= merge.adf3,
              tab.adf4= merge.adf4
              tab.adf5= CURDATE()                  
WHEN NOT MATCHED THEN
    INSERT (TSPK,adf1,adf2,adf3,adf4,adf5 )
    VALUES (merge.TSPK, merge.adf1, merge.adf2,merge.adf3,merge.adf4,CURDATE())

The error it throws is

 [SQL0104] Token MERGE was not valid. Valid tokens: ( CL END GET SET CALL DROP FREE HOLD LOCK OPEN WITH ALTER. [SQL State=42601, DB Errorcode=-104] 

In DB2, it shows we can use MERGE keyword. https://www.ibm.com/developerworks/community/blogs/SQLTips4DB2LUW/entry/merge?lang=en

Merge was added to DB2 for i on IBM i version 7.1. If you are on an earlier version (6.1 or earlier) not only is it out of service, but you can't use the MERGE statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM