MySQL中的触发器语句出错

时间:2013-06-01 09:48:51

标签: php mysql sql triggers phpmyadmin

我试图在mysql触发器中执行以下语句(通过phpmyadmin)

FOR EACH ROW BEGIN
DELETE from balancetable WHERE triggeredby="salesinvoices" AND invoiceNumber=NEW.invoiceNumber;

INSERT INTO balancetable SET invoiceNumber=NEW.invoiceNumber,ledgerId=NEW.buyerId,date=NEW.invoiceDate,company=NEW.companyId,type="debit",triggeredby="salesinvoices";
END;

我收到此错误:

MySQL said: #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 'FOR EACH ROW BEGIN DELETE from balancetable WHERE triggeredby="salesinvoices" A' at line 1

从最后一小时开始,我一直在为此而奋斗。指出我的愚蠢:)

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须为触发器命名并定义何时触发它:

delimiter |
CREATE TRIGGER `some_name` AFTER UPDATE ON `salesinvoices`
FOR EACH ROW BEGIN
    DELETE from balancetable 
    WHERE triggeredby="salesinvoices" 
    AND invoiceNumber=NEW.invoiceNumber;

    INSERT INTO balancetable SET        
          invoiceNumber=NEW.invoiceNumber,
          ledgerId=NEW.buyerId,
          date=NEW.invoiceDate,
          company=NEW.companyId,
          type="debit",
          triggeredby="salesinvoices";
END;
| 
delimiter ;