I'm trying to insert into a table if the value doesn't exist by using IF EXISTS THEN
. Everytime I try to execute this query
DELIMITER //
CREATE PROCEDURE addnTag
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt')
SELECT last_insert_id();
END IF;
END//
DELIMITER ;
I got the
error (1064) 'BEGIN IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt')) THEN SELECT -1; ELSE' at line 2 '
I can't find where is the error in my query.
Please help me. Thanks.
I think you are just missing the parentheses after the procedure name, and the semi-colon after your INSERT
statement.
This should work:
DELIMITER //
CREATE PROCEDURE addnTag()
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN
SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt');
SELECT last_insert_id();
END IF;
END//
DELIMITER ;