繁体   English   中英

MySQL存储过程问题,如果if else语句

[英]MySQL Stored Procedures issue with if then else statements

我需要将更改存储在我们的发票系统中,以将其导出到帐户系统(这是第三方应用程序)。

我想做的是添加两个触发器。

  1. 插入时:添加了新发票,必须在另一个表中将其标记为新发票,因此在下一次迁移中,生成适当的ASCII以便将其导入会计系统。

  2. 更新:这有点复杂,当发票被修改或发票已付款/或标记为已付款并最终没有发票时,可能会发生这种情况。

两个触发器调用相同的过程。

DROP PROCEDURE IF EXISTS `marca_factura_modificada`;
DELIMITER |
CREATE PROCEDURE `marca_factura_modificada`( IN nempresa_in int, IN nfactura_in int, IN cany_in char(2), IN cobrada boolean  )
BEGIN
    DECLARE existeix_factura INT;
    DECLARE abans_afegir_factura INT;
    DECLARE abans_afegir_cobrament INT;

    SELECT NFactura,afegir_factura,afegir_cobrament 
               INTO existeix_factura,abans_afegir_factura,abans_afegir_cobrament 
               FROM factures_modificades 
               WHERE NEmpresa = nempresa_in 
                     AND NFactura = nfactura_in 
                     AND CAny = cany_in 
                     LIMIT 1;
    IF existeix_factura IS NULL THEN
        IF new.DataFactura = CURDATE() THEN
            IF (new.LComptat = 1 OR new.LCreditCobrat = 1) THEN
                INSERT INTO factures_modificades (NEmpresa, NFactura, CAny,afegir_factura,afegir_cobrament ) 
                    VALUES (nempresa_in, nfactura_in, cany_in,1,1);
            ELSE
                INSERT INTO factures_modificades (NEmpresa, NFactura, CAny,afegir_factura,afegir_cobrament ) 
                    VALUES (nempresa_in, nfactura_in, cany_in,1,0);
            END IF
        ELSE
            /* Si no és d'avui i no hi ha registre es que ja es va afegir la factura en el seu dia */
            INSERT INTO factures_modificades (NEmpresa, NFactura, CAny,afegir_factura,afegir_cobrament ) 
            VALUES (nempresa_in, nfactura_in, cany_in,0,1);
        END IF
    ELSE
        IF(cobrada = 0 AND abans_afegir_factura = 0) THEN
            DELETE FROM factures_modificades WHERE NEmpresa = nempresa_in AND NFactura = nfactura_in AND CAny = cany_in LIMIT 1;
        ELSEIF (cobrada = 1 AND abans_afegir_cobrament = 0) THEN
            UPDATE factures_modificades SET afegir_cobrament =  1 WHERE NEmpresa = nempresa_in AND NFactura = nfactura_in AND CAny = cany_in;
        ELSEIF (cobrada = 0 AND abans_afegir_cobrament = 1) THEN
            UPDATE factures_modificades SET afegir_cobrament = 0 WHERE NEmpresa = nempresa_in AND NFactura = nfactura_in AND CAny = cany_in;
        END IF
    END IF
END
|
DELIMITER ;

但这在mysql 5.5上不起作用(我认为IF THEN ELSE代码中存在一些问题,但是我看不到哪里。

解决了!

现在可以了。 问题是END IF要一个; 到底。

DROP PROCEDURE IF EXISTS `marca_factura_modificada`;
DELIMITER |
CREATE PROCEDURE `marca_factura_modificada`( IN nempresa_in int, IN nfactura_in int, IN cany_in char(2), IN cobrada boolean,IN DataFactura date  )
BEGIN
    DECLARE existeix_factura INT;
    DECLARE abans_afegir_factura INT;
    DECLARE abans_afegir_cobrament INT;
    SELECT NFactura,afegir_factura,afegir_cobrament 
               INTO existeix_factura,abans_afegir_factura,abans_afegir_cobrament 
               FROM factures_modificades 
               WHERE NEmpresa = nempresa_in 
                     AND NFactura = nfactura_in 
                     AND CAny = cany_in 
                     LIMIT 1;
    IF (existeix_factura) IS NULL THEN 
            IF (DataFactura = CURDATE()) THEN 
                IF (cobrada) THEN INSERT INTO factures_modificades (NEmpresa, NFactura, CAny,afegir_factura,afegir_cobrament ) VALUES (nempresa_in, nfactura_in, cany_in,1,1);
                ELSE INSERT INTO factures_modificades (NEmpresa, NFactura, CAny,afegir_factura,afegir_cobrament ) VALUES (nempresa_in, nfactura_in, cany_in,1,0);
                END IF;
            ELSE INSERT INTO factures_modificades (NEmpresa, NFactura, CAny,afegir_factura,afegir_cobrament )  VALUES (nempresa_in, nfactura_in, cany_in,0,1);
            END IF;
    ELSE
        IF(cobrada = 0 AND abans_afegir_factura = 0) THEN DELETE FROM factures_modificades WHERE NEmpresa = nempresa_in AND NFactura = nfactura_in AND CAny = cany_in LIMIT 1;
        ELSEIF (cobrada = 1 AND abans_afegir_cobrament = 0) THEN UPDATE factures_modificades SET afegir_cobrament =  1 WHERE NEmpresa = nempresa_in AND NFactura = nfactura_in AND CAny = cany_in;
        ELSEIF (cobrada = 0 AND abans_afegir_cobrament = 1) THEN UPDATE factures_modificades SET afegir_cobrament = 0 WHERE NEmpresa = nempresa_in AND NFactura = nfactura_in AND CAny = cany_in;
        END IF;
    END IF;
END
|
DELIMITER ;

暂无
暂无

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

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