0

I am writing a trigger in a MySQL to conditionally create a new record in another table based on the value of field in newly added record in a table. Here is the script for the trigger (which should be self explanatory):

CREATE TRIGGER TRG_CREATE_INT_SRV_ROW
AFTER INSERT ON survey_details
FOR EACH ROW
BEGIN
 IF(New.LossTo == 'Vehicle') THEN
 INSERT INTO survey_vehicle (RefNo) VALUES (New.RefNo);
 ELSE
 INSERT INTO survey_other (RefNo) VALUES (New.RefNo);
 END IF;
END;

While running the query, the error message displayed is:

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 '= 'Vehicle') THEN INSERT INTO interim_survey_vehicle (RefNo) VALUES (New.RefNo' at line 5

I am writing this kind of conditional trigger for the first time.

What is wrong with code please?

McNets
24k11 gold badges51 silver badges90 bronze badges
asked Feb 26, 2018 at 15:07

1 Answer 1

1

This is very straightforward.

There is no == operator.

Please change

IF(New.LossTo == 'Vehicle') THEN

to

IF(New.LossTo = 'Vehicle') THEN

UPDATE 2018年02月26日 11:04 EST

You need to change the delimiter before and after the trigger:

DELIMITER $$
DROP TRIGGER IF EXISTS TRG_CREATE_INT_SRV_ROW $$
CREATE TRIGGER TRG_CREATE_INT_SRV_ROW
 AFTER INSERT ON survey_details
 FOR EACH ROW
BEGIN
 IF(New.LossTo = 'Vehicle') THEN
 INSERT INTO survey_vehicle (RefNo) VALUES (New.RefNo);
 ELSE
 INSERT INTO survey_other (RefNo) VALUES (New.RefNo);
 END IF;
END $$
DELIMITER ;

Please see my old post Unique combination key MySQL for an example

RDFozz
11.7k4 gold badges25 silver badges38 bronze badges
answered Feb 26, 2018 at 15:30
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.