Reputation: 89
I'm trying to set up a database that logs search terms. For each search term entered SQL checks if the search term exists, if not it INSERTS a new record, if true it UPDATES the existing field
IF (NOT EXISTS (SELECT * FROM searchmetrics WHERE keyword = 'keyword'))
BEGIN
INSERT INTO searchmetrics(id, name, metric)
VALUES('', 'keyword', '1')
END
ELSE
BEGIN
UPDATE searchmetrics
SET metrics = metrics + 1
WHERE name = 'keyword'
END
I'm getting following error:
Unrecognized statement type. (near "IF" at position 0)
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
I've double-checked the syntax and it looks correct to me, the IF statement is also highlighted red in the SQL console but seems to be the only thing that is. Can anyone tell me where I'm going wrong?
Upvotes: 1
Views: 3310
Reputation: 142296
The IF
statement must be inside a Stored Routine. If it is not, it cannot be made to work.
If it is in a SP, please provide the Routine declaration, including DELIMITER
statements.
Upvotes: 1
Reputation: 8384
With MariaDB, IF
and IF()
have different semantics. It seems you wanted to use IF
when you actually used IF()
.
Upvotes: 2