MySQL错误#1064

时间:2011-08-11 21:06:20

标签: mysql mysql-error-1064

尝试在下面创建触发器时,我收到此消息

#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 '; END' at line 9

有人可以帮忙吗?谢谢

delimiter $$

CREATE TRIGGER tr_update_item_status
AFTER UPDATE ON check_in 
FOR EACH ROW
BEGIN
  IF OLD.quantity > 0 AND NEW.quantity <= 0 THEN
    UPDATE check_in SET item_status = 'Sold';
  ELSEIF OLD.quantity <= 0 AND NEW.quantity > 0 THEN
    UPDATE check_in SET item_status = 'Available';
  ENDIF;
END;$$

delimiter ;

2 个答案:

答案 0 :(得分:3)

DELIMITER $$

CREATE TRIGGER tr_update_item_status
AFTER UPDATE ON check_in  
FOR EACH ROW
BEGIN
  IF OLD.quantity > 0 AND NEW.quantity <= 0 THEN
    UPDATE check_in SET item_status = 'Sold';
  ELSEIF OLD.quantity <= 0 AND NEW.quantity > 0 THEN
    UPDATE check_in SET item_status = 'Available';
  END IF;
END $$

DELIMITER ;

这应该有效。一切顺利。

答案 1 :(得分:1)

DELIMITER $$

CREATE TRIGGER tr_update_item_status
BEFORE UPDATE ON check_in 
FOR EACH ROW
BEGIN
  IF OLD.quantity > 0 AND NEW.quantity <= 0 THEN
    SET NEW.item_status = 'Sold';
  ELSEIF OLD.quantity <= 0 AND NEW.quantity > 0 THEN
    SET NEW.item_status = 'Available';
  END IF;
END; $$

DELIMITER ;

三(3)件事:

  • ENDIF;应该是END IF;
  • 这应该是BEFORE UPDATE触发器,因为在任何表操作之前数据验证更好
  • UPDATE语句在语义上是错误的。我更改了它们以设置新列名称