0

I am trying to create a mysql trigger. This is what I am trying to create, but somehow it tells me that: "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 'A FOR EACH ROW".

What Am I doing wrong here? I have tried to naming them differently, but it does not seem to work.

delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock` A
       FOR EACH ROW
       BEGIN
           IF NEW.`quantity` < 1 THEN //belongs to alias A
               UPDATE `ps_product` B
               SET B.`visibility` = 'none';
               WHERE id_product = OLD.id_product  //Should be the id_product of A alias
           ELSEIF NEW.quantity > 0 THEN
               UPDATE `ps_product` C
               SET C.`visibility` = 'both';
               WHERE id_product = OLD.id_product //Should be the id_product of A alias
           END IF;
       END;//
delimiter ;
1
  • 1
    What do you expect the "A" at the end of your second line to do? The comments in the code suggest you're thinking of it as an alias. You can not alias the table the trigger is on there. Also, MySQL comments start with # or --. Commented Oct 4, 2019 at 23:21

1 Answer 1

1

Try this one

delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock` 
       FOR EACH ROW
       BEGIN
           IF NEW.`quantity` < 1 THEN #belongs to alias A
               UPDATE `ps_product` B
               SET B.`visibility` = 'none'
               WHERE id_product = OLD.id_product;  #Should be the id_product of A alias
           ELSEIF NEW.quantity > 0 THEN
               UPDATE `ps_product` C
               SET C.`visibility` = 'both'
               WHERE id_product = OLD.id_product; #//Should be the id_product of A alias
           END IF;
       END;//
delimiter ;

This shows no errors in mysql workbbench

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.