Question
· Jan 14

INSERT OR UPDATE WITH A COUNTER

Hello,

So i want to use the INSERT OR UPDATE command so i can update a COUNTER for a given name:

 

INSERT OR UPDATE myTable
SET name='Omer',  counter = counter + 1;

 


as you can see with the above code - if the row is non-existent then we get an error because COUNTER is NULL! 
I tried the following to fix this but all have failed:

 


INSERT OR UPDATE myTable
SET name = 'Omer', 
    counter = CASE 
        WHEN counter IS NULL THEN 1 
        ELSE counter + 1
    END

 


INSERT OR UPDATE myTable SET name='Omer',counter = COALESCE(counter + 1, 1)

 

 

INSERT OR UPDATE myTable SET name='Omer',counter = IFNULL(counter + 1, 1)

For any of the solutions above i received the error:
 

  [SQLCODE: <-400>:<Fatal error occurred>]

  [%msg: <Unexpected error occurred: <UNDEFINED>%0Ao+3^%sqlcq.74.1 *sqldatad(3)>]

 

Side Note: Running the query below does work:

 

INSERT OR UPDATE myTable SET name='Omer',counter= 1

So this implies that problem is indeed with using the counter when it is NULL...

 

 

Would love to get some insight on that, Thx!

Product version: Caché 2018.1
Discussion (1)2
Log in or sign up to continue