简体   繁体   English

在创建触发器之前,DELIMITER上的MySQL语法错误

[英]MySQL Syntax Error on DELIMITER before CREATE TRIGGER

I'm trying to rewrite a Trigger that I made with Firebird to a MySql Trigger. 我正在尝试将用Firebird制作的触发器重写为MySql触发器。

I realy have no idea what could be. 我真的不知道会是什么。 If anyone could help me... thanks 如果有人可以帮助我...谢谢

I'm submiting the SQL with PHP just like follows, and the error message is: 我正在使用PHP提交SQL,如下所示,错误消息是:

Invalid query: You have an error in your SQL syntax; 查询无效:您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ CREATE TRIGGER FP_PAGO_AI AFTER INSERT ON FORMA_PGTO FOR EACH R' at line 1 在第1行的“ DELIMITER $$ CREATE TRIGGER FP_PAGO_AI AFTER INSERT ON FORMA_PGTO FOR EACH R”附近检查与您的MySQL服务器版本相对应的手册以使用正确的语法

The trigger's code is: 触发器的代码是:

$str[] = "
DELIMITER $$

CREATE TRIGGER FP_PAGO_AU AFTER UPDATE ON FORMA_PGTO
FOR EACH ROW
BEGIN

    declare rec_count integer;
    declare pg_count integer;
    declare cp_pago integer;

    select count(*) from forma_pgto fp where fp.id_cpagar=new.id_cpagar into rec_count;
    select count(pago) from forma_pgto f where f.id_cpagar=new.id_cpagar and f.pago=1 into pg_count;

    /* Se todas parcelas estao pagas, entao setar conta paga */
    if (rec_count = pg_count) then
        update cpagar c set c.pago=1 where c.id=new.id_cpagar;
    /* Senao */
    else
        /* Se CPAGAR.PAGO = 1, recebe 0 */
        select cpg.pago from cpagar cpg where cpg.id=new.id_cpagar into cp_pago;
        if (cp_pago = 1) then /* Se cp_pago = 1 */
            update cpagar set pago=0 where id=new.id_cpagar;
        end if;
    end if;

END

END $$

DELIMITER ;
";

You don't need DELIMITER $$ at all. 您根本不需要DELIMITER $$ That's a mysql client builtin command . 那是一个mysql客户端内置命令 Client builtins are not recognized by the SQL parser. SQL解析器无法识别客户端内置文件。

You can just execute the CREATE TRIGGER statement as a single statement and then you don't need to have a delimiter at the end of the statement. 您可以只将CREATE TRIGGER语句作为单个语句执行,然后在语句末尾不需要定界符。 Delimiters are only important in interfaces that support multiple statements (eg the mysql client). 分隔符仅在支持多个语句的接口(例如mysql客户端)中很重要。

phpMyAdmin also permits multiple statements, so you do need to set the delimiter, but this is done with a user interface control, not the DELIMITER command. phpMyAdmin还允许多个语句,因此您确实需要设置定界符,但这是通过用户界面控件而不是DELIMITER命令来完成的。 See Store procedures in phpMyAdmin 请参阅phpMyAdmin中的存储过程

Execute it directly in PhpAdmin and remove one redundant END 直接在PhpAdmin中执行它并删除一个冗余END

DELIMITER $$

CREATE TRIGGER FP_PAGO_AU AFTER UPDATE ON FORMA_PGTO
FOR EACH ROW
BEGIN

    declare rec_count integer;
    declare pg_count integer;
    declare cp_pago integer;

    select count(*) from forma_pgto fp where fp.id_cpagar=new.id_cpagar into rec_count;
    select count(pago) from forma_pgto f where f.id_cpagar=new.id_cpagar and f.pago=1 into pg_count;

    /* Se todas parcelas estao pagas, entao setar conta paga */
    if (rec_count = pg_count) then
        update cpagar c set c.pago=1 where c.id=new.id_cpagar;
    /* Senao */
    else
        /* Se CPAGAR.PAGO = 1, recebe 0 */
        select cpg.pago from cpagar cpg where cpg.id=new.id_cpagar into cp_pago;
        if (cp_pago = 1) then /* Se cp_pago = 1 */
            update cpagar set pago=0 where id=new.id_cpagar;
        end if;
    end if;

END $$

DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM