MySQL分隔符语句错误

时间:2014-09-01 10:44:16

标签: mysql playframework

我正在尝试在mysql中运行以下脚本:

DELIMITER $$$
DROP TRIGGER IF EXISTS invoice_line_insert
$$$
CREATE TRIGGER invoice_line_insert AFTER INSERT
ON invoice_line FOR EACH ROW
BEGIN
    IF NEW.type = "DELIVERY" THEN
        UPDATE invoice
        SET invoice.etdelivery_amount = invoice.etdelivery_amount + NEW.amount
        WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;
    ELSE
        UPDATE invoice
        SET invoice.etexpense_amount = invoice.etexpense_amount + NEW.amount
        WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;
    END IF;
    UPDATE invoice
    SET invoice.vatamount = (NEW.amount * ((
                SELECT vat.rate
                FROM vat
                WHERE vat.id_vat = NEW.vat_id_vat
    ) / 100)) + invoice.vatamount
    WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;

    UPDATE invoice
    SET invoice.itamount = invoice.vatamount +
            invoice.etdelivery_amount +
            invoice.etexpense_amount
    WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;
END
$$$

当我在mySql Workbench中运行它时,它工作正常,但是当播放2以自动方式运行它(在一个名为2.sql的文件中)时,我收到以下错误:

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在正确的语法附近使用DELILITER $$$ DROP TRIGGER IF EXISTS invoice_line_insert $$$ CREATE TRIGGER invo'在第1行[错误:1064,SQLSTATE:42000],尝试运行此SQL脚本时:

我在网上看到,分隔符语句只适用于特定的gui,而不是每次都有。真的吗 ?为什么?如何解决它,因为我需要分隔符语句?

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

这似乎是Play framework 2.0 evolutions and create trigger的副本 (请注意,在我看来,更好的答案是Roger在2013年5月24日发布的答案,即上面的链接)

“delimiter”不能用于进化脚本文本;我似乎无法找到任何文件,为什么会这样。但也许这与“delimiter”不是SQL语句而是SQL属性这一事实有关。

但是,Evolutions section of Play 2 docs中有一个解决方案:

Play将.sql文件拆分为一系列以分号分隔的语句,然后逐个对数据库执行它们。因此,如果您需要在语句中使用分号,请输入;;代替 ;。例如,INSERT INTO标点符号(名称,字符)VALUES('分号',';;');。

所以在你的情况下,

  1. 删除“分隔符”属性,
  2. 使用“;;”代替 ”;”对于内部 SQL语句,以防止Play 2解析器单独执行这些内部SQL语句。
  3. 以下是我在Play 2.3和mysql 14.14 Distrib 5.5.40(Ubuntu 12.04LTS)中成功测试的示例:

    DROP TRIGGER IF EXISTS SOFTWARE_INSERT_CT_TRIGGER;
    CREATE TRIGGER SOFTWARE_INSERT_CT_TRIGGER
    BEFORE INSERT ON SOFTWARE
    FOR EACH ROW
    BEGIN
      IF NEW.CREATED_TIME = '0000-00-00 00:00:00' THEN
        SET NEW.CREATED_TIME = NOW();;
      END IF;;
    END;
    

    对于SQL脚本,以下内容适用于Play 2.1及更高版本(请注意,我尚未对其进行测试):

    DROP TRIGGER IF EXISTS invoice_line_insert;
    CREATE TRIGGER invoice_line_insert AFTER INSERT
    ON invoice_line FOR EACH ROW
    BEGIN
      IF NEW.type = "DELIVERY" THEN
        UPDATE invoice
        SET invoice.etdelivery_amount = invoice.etdelivery_amount + NEW.amount
        WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
      ELSE
        UPDATE invoice
        SET invoice.etexpense_amount = invoice.etexpense_amount + NEW.amount
        WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
      END IF;;
      UPDATE invoice
      SET invoice.vatamount = (NEW.amount * ((
        SELECT vat.rate
        FROM vat
        WHERE vat.id_vat = NEW.vat_id_vat
      ) / 100)) + invoice.vatamount
      WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
    
      UPDATE invoice
      SET invoice.itamount = invoice.vatamount +
        invoice.etdelivery_amount +
        invoice.etexpense_amount
      WHERE invoice.id_invoice = NEW.invoice_parent_id_invoice;;
    END;