Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL存储过程出现if-then-else语句问题_Mysql_Procedures - Fatal编程技术网

MySQL存储过程出现if-then-else语句问题

MySQL存储过程出现if-then-else语句问题,mysql,procedures,Mysql,Procedures,我需要在我们的发票系统中存储更改以将其导出到帐户系统(这是第三方应用程序) 我想做的是添加两个触发器 插入时:添加新发票时,必须在另一个表中将其标记为新发票,因此在下一次迁移中,生成相应的ASCII以将其导入会计系统 更新时:这有点复杂,当发票被修改或发票已付款/或已标记为已付款但最终未付款时,可能会发生这种情况 两个触发器调用相同的过程 DROP PROCEDURE IF EXISTS `marca_factura_modificada`; DELIMITER | CREATE PROCEDU

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

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

  • 插入时:添加新发票时,必须在另一个表中将其标记为新发票,因此在下一次迁移中,生成相应的ASCII以将其导入会计系统

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

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

    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代码中有一些问题,但我不知道在哪里。

    解决了

    现在它起作用了。问题是如果想要一个,就结束;最后

    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 ;
    

    这是一个复杂的问题。存储过程有很多种“无法工作”的方式。请详细说明您遇到的问题。问题出现在“插入到factures_修改(NEmpresa,NFactura,CAny,afegir_factura,afegir_cobrament)值(NEmpresa_in,NFactura_in,CAny_in,0,1);”我不知道,但我认为IF-THEN-else语句是不好的…有没有办法将它们分开?我的意思是像php中的IF(something){some_stuff()}else IF(something){that()}else{other_stuff()}