IF NOT EXISTS - Mariadb Syntax

Viewed 13368

i tried to either insert a new row or update an existing one if it is already included.

I figured i could try it with indices but to be honest i am not sure how to do this hence i tried to do it with an IF statement. Unfortunatly this isn't working either because it says i have a problem with my Syntax. I am using MariaDB.

IF NOT EXISTS (SELECT * FROM valuation 
               WHERE ticker = 'BK001EUR' AND depot_id =1 
                 AND src_id =2 AND valuation_date ='2009-09-09')
INSERT INTO valuation (ticker,depot_id,src_id,valuation_date,value) 
VALUES ('BK001EUR',1,2,'2009-09-09',14999260.46) 
ELSE 
UPDATE valuation 
SET value =14999260.46 
WHERE ticker = 'BK001EUR' AND depot_id =1 
  AND src_id =2 AND valuation_date ='2009-09-09');
3 Answers

I tried a similar INSERT-statement, which should not insert the data, if a record with the same values exists. My statement didn't work until I added 'FROM DUAL':

INSERT INTO measurement
    (sensorid, parameterid, measuredatetime, measure)
    SELECT sensoridIn,parameteridIn,measuredatetimeIn,measureIn
    FROM DUAL
    WHERE NOT EXISTS (SELECT *
          FROM measurement
          WHERE sensorid = sensorId
          AND measuredatetime = measuredatetimeIn
          AND measure = measureIn
          AND parameterid = parameteridIn);

For more info see https://mariadb.com/kb/en/dual/ and How can I do 'insert if not exists' in MySQL?

Related