2013年10月1日 星期二

msql-procedure http://stackoverflow.com/questions/12228463/mysql-stored-procedures-issue-with-if-then-else-statements





in : http://stackoverflow.com/questions/12228463/mysql-stored-procedures-issue-with-if-then-else-statements

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 ;

沒有留言:

張貼留言