-1

I am trying to pass following query to dashdb:

IF (NOT EXISTS (SELECT * FROM TEST_CONFIGURATION WHERE DEVICEID = 'OZ_POLLUDRON_010')) 
BEGIN 
INSERT INTO TEST_CONFIGURATION (DEVICEID, DEVICETYPE, SERIAL, TYPE, MACID, CONFIG, INIT) VALUES('OZ_POLLUDRON_010', 'POLLUDRON_PRO', '9428424248', 0, '200050000b51343334363138', '[object Object]', '[object Object]')
END 

ELSE 
BEGIN 
UPDATE TEST_CONFIGURATION SET DEVICEID = 'OZ_POLLUDRON_010', DEVICETYPE = 'POLLUDRON_PRO', SERIAL = '9428424248', TYPE = 0, MACID = '200050000b51343334363138', CONFIG = '[object Object]', INIT = '[object Object]' WHERE DEVICEID = 'OZ_POLLUDRON_010' END

But it is giving me following error:

database/sql/driver: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "IF" was found following " ". Expected tokens may include: "<call>". SQLSTATE=42601

Can anyone please help me out with it?

Freddy
  • 2,216
  • 3
  • 31
  • 34

1 Answers1

0

Consider using the MERGE statement to achieve an upsert action (insert if not exist else update). Examples are in the DB2 samples directory and in the DB2 Knowledge Center.

mao
  • 11,321
  • 2
  • 13
  • 29
  • can you please share links of examples in answer? – Freddy Jul 13 '17 at 07:58
  • Syntax for MERGE statement in DB2 midrange (examples are near the end of the page ) https://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0010873.html – mao Jul 13 '17 at 08:05