2012-09-01 87 views
-1

我需要將更改存儲在我們的發票系統中以將其導出到帳戶系統(這是第三方應用程序)。MySQL存儲過程與if then else語句的問題

我想要做的是添加兩個觸發器。

  1. ON INSERT:一個新的發票時,它必須被標記爲另一臺新的,所以在接下來的遷移,產生此時,相應的ASCII導入它的會計制度。

  2. ON UPDATE:這有點複雜,這可能發生在發票被修改或者發票被支付/或者被標記爲支付並且最終沒有支付時。

兩個觸發器都調用相同的過程。

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代碼中的一些問題,但我沒有看到哪裏。

+0

這是一個複雜的問題,有一個存儲過程可以「不工作」很多,很多方面請詳細說明是什麼問題,您有 –

+0

。。問題出現在「INSERT INTO factures_modificades(NEmpresa,NFactura,CAny,afegir_factura,afegir_cobrament)VALUES(nempresa_in,nfactura_in,cany_in,0,1);」這個在第二個else之後。我不知道,但我認爲哪個是壞的是IF THEN ELSE聲明......有沒有辦法將它們分開?我的意思是像在PHP中,如果(某事){some_stuff(); } else if(something_else){that();} else {another_stuff();} – MiQUEL

回答

4

解決!

現在,它的工作原理,但問題是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 ;