1

Im running the following query from my PhpMyadmin sql tab

CREATE TRIGGER testTrigger 
AFTER INSERT ON tbl_table1 
FOR EACH ROW 
BEGIN 
INSERT INTO tbl_table2 (id,name) values (NEW.id,NEW.name); 
END

But everytime I'm getting this error msg:

#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 5

Im using MySQL client version: 5.1.37,PHPMyadmim Version information: 3.2.2.1deb1

0

1 Answer 1

4

Look here:

When you want to use SET NEW.col_name = value in your trigger, please note that you CANNOT use this with the AFTER the action, and must use it BEFORE the action.

So probably you should try:

CREATE TRIGGER testTrigger 
BEFORE INSERT ON tbl_table1 
FOR EACH ROW 
BEGIN 
    INSERT INTO tbl_table2 (id,name) values (NEW.id,NEW.name); 
END

EDITED: I tried this and it's working in MySQL

DROP TRIGGER IF EXISTS testTrigger;
CREATE TRIGGER testTrigger 
BEFORE INSERT ON tbl_table1 
FOR EACH ROW 
BEGIN 
  INSERT INTO tbl_table2 VALUES (NEW.id,NEW.name);
END;

Then tried INSERT INTO test1 SELECT 5,'pois'; and trigger worked!!

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

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.