0

please help i have trigger code below

DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN    
IF (
    SELECT table1.idtable1  FROM table2, table1, table 
    WHERE table1.idtable1=table2.idtable2
    and table0.idtable0=table1.idtable1
)
THEN
    UPDATE targettable 
    SET targettable.column = 1
    WHERE targettable.idtable=table1.idtable1;
END IF;

END$$

after running it, it shows error

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 17

how to fix it? MySQL version is 5.5.34

2
  • SELECT table1.idtable1 FROM table2, table1, table <- should the last table be table0? Commented Apr 26, 2015 at 18:23
  • What is the trigger supposed to do? Commented Apr 26, 2015 at 18:40

2 Answers 2

1

table is a reserved word in mysql. Surround table with backticks. Also you were missing delimiter and a semicolon at the end. This works:

DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN    
IF (
    SELECT table1.idtable1  FROM table2, table1, `table` 
    WHERE table1.idtable1=table2.idtable2
    and table0.idtable0=table1.idtable1
)
THEN
    UPDATE targettable 
    SET targettable.column = 1
    WHERE targettable.idtable=table1.idtable1;
END IF;
END;$$
delimiter ;

I hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

1
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN    
IF (
    SELECT table1.idtable1  FROM table2, table1, table <-- you should escape this using backticks as this is a reserved word i.e., `table`
    WHERE table1.idtable1=table2.idtable2
    and table0.idtable0=table1.idtable1
)
THEN
    UPDATE targettable 
    SET targettable.column = 1
    WHERE targettable.idtable=table1.idtable1;
END IF;

END$$

Comments

Your Answer

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.