2

Here is my trigger, I am getting the MySQL syntax error. I wanted to reduce the balance from sms_index table sms_count column value.

 CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
  FOR EACH ROW
  BEGIN
    IF NEW.approved_status = '1' THEN
      UPDATE  sms_package SET  balance =  (balance - OLD.sms_count) WHERE  group_id = OLD.ins_group_id;
    END IF;
  END;

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 5 
1
  • You may not have set delimiters If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.-dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html Commented Feb 10, 2018 at 9:07

1 Answer 1

6

Your code looks correct, except for the possible problem of the delimiter. Try the following:

DELIMITER //
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
    IF NEW.approved_status = '1' THEN
        UPDATE  sms_package SET  balance =  (balance - OLD.sms_count)
        WHERE  group_id = OLD.ins_group_id;
    END IF;
END;//

DELIMITER ;

From the documentation:

However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition.

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

6 Comments

now I am getting just "Error" no error id and error type. is it the problem with MySQL version?
I see no errors in the trigger I wrote above. Try running an empty trigger and see what happens. If an empty trigger still generates an error message then something else is at work here.
I generate the Trigger from PHPMyadmin its works, I'll check the generated query, it doesn't have the Begin and End comment. do you know why?
Good question...I don't know why.
oh my god, you are saying don't know, your Reputations says you are "G" :)
|

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.