Connecting Tech Pros Worldwide Help | Site Map

Problem in trigger

Familiar Sight
 
Join Date: Nov 2007
Posts: 233
#1: Feb 20 '08
Hi

I am new to Trigger. I write trigger to insert total no.of row of a table to another table in this way

CREATE TRIGGER count_row
AFTER INSERT ON main_table
FOR EACH ROW BEGIN
INSERT INTO row_count_table (after_insert) (SELECT COUNT(*) FROM main_table);

I don't know what is the wrong in this statement while I tried to end this statement with END; also.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Feb 20 '08

re: Problem in trigger


Quote:

Originally Posted by mukeshrasm

Hi

I am new to Trigger. I write trigger to insert total no.of row of a table to another table in this way

CREATE TRIGGER count_row
AFTER INSERT ON main_table
FOR EACH ROW BEGIN
INSERT INTO row_count_table (after_insert) (SELECT COUNT(*) FROM main_table);

I don't know what is the wrong in this statement while I tried to end this statement with END; also.


There you go.
Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#3: Feb 20 '08

re: Problem in trigger


Try this:

Expand|Select|Wrap|Line Numbers
  1.  DELIMITER $
  2. CREATE TRIGGER count_row 
  3. AFTER INSERT ON main_table
  4. FOR EACH ROW INSERT INTO row_count_table SELECT COUNT(*) FROM main_table;
  5. $
Familiar Sight
 
Join Date: Nov 2007
Posts: 233
#4: Feb 20 '08

re: Problem in trigger


Quote:

Originally Posted by chaarmann

Try this:

DELIMITER $$
CREATE TRIGGER count_row
AFTER INSERT ON main_table
FOR EACH ROW INSERT INTO row_count_table SELECT COUNT(*) FROM main_table;
$$

I tried this as but I got this error message:

#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 4
Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#5: Feb 23 '08

re: Problem in trigger


Quote:

Originally Posted by mukeshrasm

I tried this as but I got this error message:

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

What editor are you using to run the SQL?
And why is it saying '"' in the error message? There is no double quotation mark inside the SQL! Did you add some somewhere?
Familiar Sight
 
Join Date: Nov 2007
Posts: 233
#6: Feb 23 '08

re: Problem in trigger


I am using PhpMyAdmin to run the SQL. And there is no double quote in the sql but why it is giving such error that I don't know. So if you can help me and try to run this query on your own and find whether it is working or not. Ok




Quote:

Originally Posted by chaarmann

What editor are you using to run the SQL?
And why is it saying '"' in the error message? There is no double quotation mark inside the SQL! Did you add some somewhere?

Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#7: Feb 24 '08

re: Problem in trigger


Quote:

Originally Posted by mukeshrasm

I am using PhpMyAdmin to run the SQL. And there is no double quote in the sql but why it is giving such error that I don't know. So if you can help me and try to run this query on your own and find whether it is working or not. Ok

My query is running fine. I just copied it again from this forum and run it, just to verify there was no mistyping by accident.
I don't know about PhpAdmin, maybe it's PhpAdmin that is doing something weird with the query when sending it to mySql, like special character replacement.
I am using SQLYog. It's free. And it supports triggers. Give this program a try and see if it runs the trigger-SQL without error. I was also creating triggers with Java using JDBC and it was running fine. So if you have still problems even after using SQLYog, which supports you in a way that it writes a full trigger template for you automatically after you click a button on a table, then there is something wrong with your mySql database.
Are you sure that you are accessing the correct mySql instance? In the beginning I installed mySql 4.2 (which cannot handle triggers) and then I installed mySql 5.0.41 (which can handle triggers), but the mySql 4.2 instance was still running and I was accessing it in one of my programs unintentional, so triggers did not run, and no data seemed to be updated by normal queries. Only after I deinstalled the 4.2 instance manually, my program was automatically connecting to mySql 5.0.41 instance and then the trigger creation worked.
Familiar Sight
 
Join Date: Nov 2007
Posts: 233
#8: Feb 26 '08

re: Problem in trigger


Quote:

Originally Posted by chaarmann

My query is running fine. I just copied it again from this forum and run it, just to verify there was no mistyping by accident.
I don't know about PhpAdmin, maybe it's PhpAdmin that is doing something weird with the query when sending it to mySql, like special character replacement.
I am using SQLYog. It's free. And it supports triggers. Give this program a try and see if it runs the trigger-SQL without error. I was also creating triggers with Java using JDBC and it was running fine. So if you have still problems even after using SQLYog, which supports you in a way that it writes a full trigger template for you automatically after you click a button on a table, then there is something wrong with your mySql database.
Are you sure that you are accessing the correct mySql instance? In the beginning I installed mySql 4.2 (which cannot handle triggers) and then I installed mySql 5.0.41 (which can handle triggers), but the mySql 4.2 instance was still running and I was accessing it in one of my programs unintentional, so triggers did not run, and no data seemed to be updated by normal queries. Only after I deinstalled the 4.2 instance manually, my program was automatically connecting to mySql 5.0.41 instance and then the trigger creation worked.

I am using MySql 5.0.45 instance and I was able to run the query on PhpMyAdmin. For that I had to remove the Key Word BEGIN and DELIMITER $.
Now it works fine. And I will use SQLYog as well. '
Expert
 
Join Date: Nov 2007
Location: Germany
Posts: 294
#9: Feb 26 '08

re: Problem in trigger


Quote:

Originally Posted by mukeshrasm

I am using MySql 5.0.45 instance and I was able to run the query on PhpMyAdmin. For that I had to remove the Key Word BEGIN and DELIMITER $.
Now it works fine. And I will use SQLYog as well. '

I am glad you solved the issue.

Afterthoughts:
Strange - there is no word "BEGIN" written in the SQL I listed.
I suggest you copy the SQL exactly as it is next time. I mean by using copy-and-paste, and not by trying to modify your original SQL. You are only lucky that there was no other error inside, for example an invisible character. Microsoft Frontpage once inserted an invisible 0xFF character in a JSP-page of a friend, and when he tried to run the SQL inside, it always crashed with a syntax error. It nearly drove us nuts. Because it looked like "insert into", but there actually was written "insert in(0xFFF)to" when we analyzed with a hex-editor.

The "DELIMITER" is only useful if you use a frontend like SQLYog, which otherwise would stop at the first ";" detected and not send the rest of the SQL over. So you should not use it from inside your PHP or JSP.

The "BEGIN" must be closed by an "END;" and should be used if you have more than one SQL that you need to run as a trigger event. It encloses them.
Member
 
Join Date: Jul 2007
Posts: 37
#10: Dec 4 '08

re: Problem in trigger


Im getting the above same error.
The syntax for the trigger is same as above.Checked with delimiter begin and end.
The same trigger is working fine ON Windows Mysql Query Browser 1.2.12
but when i use the same on Linux EL4 its says syntax error 1064.

Awaiting for reply.
Thanks in advance
Newbie
 
Join Date: Jan 2007
Posts: 1
#11: 1 Week Ago

re: Problem in trigger


I also get that error too. The solutions which perhaps will help you are :

1. Try to look up your trigger which have been created. use command : SHOW TRIGGERS ;

2. If i'm not wrong, your trigger has been created and you can see it there.
try to drop the trigger if the trigger has been created.

3. Try to compile it again ...


and the complete code is :

DELIMITER $$

CREATE DEFINER=`root`@`localhost` TRIGGER `trig_unit_log`
BEFORE INSERT ON tbl_unit
FOR EACH ROW
BEGIN

DECLARE upd_time DATETIME;
DECLARE upd_reason VARCHAR(45);

SET upd_time = CURDATE();
SET upd_reason = 'INSERT';

INSERT INTO syslogfiles13 (UnitUID, UnitID, UnitName, Moderator, Publish, Waktu, Keterangan)
VALUES (NEW.UnitUID, NEW.UnitID, NEW.UnitName, NEW.Moderator, NEW.Publish, upd_time, upd_reason);

END;

$$
Reply