Problem in trigger

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mukeshrasm
    Contributor
    • Nov 2007
    • 254

    Problem in trigger

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      Try this:

      [CODE=mysql] DELIMITER $
      CREATE TRIGGER count_row
      AFTER INSERT ON main_table
      FOR EACH ROW INSERT INTO row_count_table SELECT COUNT(*) FROM main_table;
      $[/CODE]
      Last edited by r035198x; Feb 20 '08, 11:17 AM. Reason: added code tags, please don't forget them next time

      Comment

      • mukeshrasm
        Contributor
        • Nov 2007
        • 254

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

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

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

          Comment

          • mukeshrasm
            Contributor
            • Nov 2007
            • 254

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




            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?

            Comment

            • chaarmann
              Recognized Expert Contributor
              • Nov 2007
              • 785

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

              Comment

              • mukeshrasm
                Contributor
                • Nov 2007
                • 254

                #8
                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. '

                Comment

                • chaarmann
                  Recognized Expert Contributor
                  • Nov 2007
                  • 785

                  #9
                  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.

                  Comment

                  • crs27
                    New Member
                    • Jul 2007
                    • 40

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

                    Comment

                    • witecloner
                      New Member
                      • Jan 2007
                      • 1

                      #11
                      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;

                      $$

                      Comment

                      Working...