MySQL Stored Procedures issue with if then else statements
-
29-06-2021 - |
题
I have the need to store the changes in our invoice system to export it to the account system (which is a 3rd party app).
What I'm trying to do is to add two triggers.
ON INSERT : A new invoice is added, it has to be marked as new in another table, so in the next migration, generate the appropiate ASCII to import it in the accounting system.
ON UPDATE: This is a bit more complicated, this can happen when an invoice is modified or when an invoice becomes payed / or it was marked to be payed and finally it hasnt.
Both Triggers call the same procedure.
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 ;
But this doesnt work on mysql 5.5 (I think there is some issue in IF THEN ELSE code, but I dont see where.
解决方案
Solved!
Now it works. The problem was END IF wants a ; in the end.
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 ;
不隶属于 StackOverflow