I would like to use an if-then-else structure to decide which of two different commands to run. However, I always get a syntax error when trying to use an if-statement.
The query should run both in MySQL and in postgresql.
IF EXISTS (SELECT * FROM Table WHERE col1 = val1 AND col2 = val2)
UPDATE Table SET col3 = val3 WHERE col1 = val1 AND col2 = val2
ELSE
INSERT INTO Table VALUES (val1, val2, val3) END IF;
What I get is ERROR: syntax error at or near "IF"
.
I have tried various versions of this, including a 'then' after the if, with or without 'end if', with 'begin' and 'end', but nothing seems to work, so I think I must be getting something very wrong. Other questions on here that I have found always included a SELECT statement in which the if-statements were embedded, which I don't seem to need.