iThink
iThink

Reputation: 1259

MySQL trigger query from phpMyAdmin

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

Upvotes: 1

Views: 11375

Answers (1)

Marco
Marco

Reputation: 57593

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!!

Upvotes: 4

Related Questions