2016-05-19 54 views
0
CREATE DEFINER=`root`@`localhost` PROCEDURE `GenerateCharges2`() 
BEGIN 
Declare sumFunding LONG; 
Declare done INT DEFAULT FALSE; 
Declare invoiceCharge LONG; 
Declare agencyID INT; 
declare invoiceID int; 
Declare fundingID INT; 
Declare amountOfFunding LONG; 
Declare getInvoiceData CURSOR For select invoices.idInvoices, invoices.idAgencies, invoices.InvoiceAmount, fundings.FundingBalance, fundings.idfundings from invoices 
inner join agencies on invoices.idAgencies = agencies.idAgencies 
inner join fundings on agencies.idAgencies = fundings.IdAgencies 
where processed =0 
group by invoices.idInvoices; 
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 


open getInvoicedata; 
ze_loop: Loop 
Fetch getInvoiceData into invoiceID, agencyID, invoiceCharge, amountOfFunding, fundingID; 
if done then leave ze_loop; 
end if; 
if amountOfFunding > invoiceCharge then 
insert into charges VALUES (invoiceCharge, invoiceID, fundingID); 
end if; 
end loop; 
close getInvoiceData; 
END 

我的問題是,如果你要插入一個select語句,它會運行一個適當的時間。但是,插入語句僅對示例數據插入一次。在任何情況下,amnountOfFunding總是比invoiceCharge大。期望的結果是所有插入都發生在獲取的行上。MySQL:程序循環,但只能插入一次

+0

您考慮過['INSERT ... SELECT'](https://dev.mysql.com/doc/en/insert-select.html)嗎? – eggyal

+0

我對你最後兩句話感到困惑,爲什麼不去掉兩個值之間的比較呢? – Uueerdo

+0

從來沒有聽說過。 MySQL/SQL新手。我會在哪裏發表這個聲明? – DefaultDefault

回答

0

我有這個循環,並插入我需要的每個記錄!也許你可以修改或更新你的循環!

DECLARE done INT DEFAULT FALSE ; 
    DECLARE l_local_formato_retail_codigo, 
    l_producto_sku, 
    l_producto_descripcion_sra, 
    l_producto_marca, 
    l_producto_categoria VARCHAR (100) ; 
    DECLARE l_precio DECIMAL (20, 7) DEFAULT 0 ; 
    DECLARE l_un_1 INT (20) DEFAULT 0 ; 
    DECLARE l_un_2 INT (20) DEFAULT 0 ; 
    DECLARE l_clp_1 DECIMAL (20, 7) DEFAULT 0 ; 
    DECLARE l_clp_2 DECIMAL (20, 7) DEFAULT 0 ; 
    DECLARE contador INT DEFAULT 0 ; 
    DECLARE valores CURSOR FOR 
    SELECT 
    local_formato_retail_codigo, 
    producto_sku, 
    producto_descripcion_sra, 
    producto_marca, 
    producto_categoria 
    FROM 
    base_comercial 
    WHERE mes_fecha = fecha 
    GROUP BY producto_sku, 
    local_formato_retail_codigo 
    ORDER BY local_formato_retail_codigo ASC, 
    producto_sku ASC ; 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE ; 
    TRUNCATE precio_cd ; 
    OPEN valores ; 
    read_loop : 
    LOOP 
    FETCH valores INTO l_local_formato_retail_codigo, 
    l_producto_sku, 
    l_producto_descripcion_sra, 
    l_producto_marca, 
    l_producto_categoria ; 
    IF done 
    THEN LEAVE read_loop ; 
    END IF ; 
    SET l_clp_1 = 
    (SELECT 
     ROUND(SUM(sellout_clp), 7) 
    FROM 
     base_comercial 
    WHERE anno = DATE_FORMAT(fecha, '%Y') 
     AND mes = DATE_FORMAT(fecha, '%c') 
     AND local_formato_retail_codigo = l_local_formato_retail_codigo 
     AND producto_sku = l_producto_sku) ; 
    SET l_clp_2 = 
    (SELECT 
     ROUND(SUM(sellout_clp), 7) 
    FROM 
     base_comercial 
    WHERE anno = DATE_FORMAT(
     DATE_ADD(fecha, INTERVAL - 1 MONTH), 
     '%Y' 
    ) 
     AND mes = DATE_FORMAT(
     DATE_ADD(fecha, INTERVAL - 1 MONTH), 
     '%c' 
    ) 
     AND local_formato_retail_codigo = l_local_formato_retail_codigo 
     AND producto_sku = l_producto_sku) ; 
    SET l_un_1 = 
    (SELECT 
     SUM(sellout_un) 
    FROM 
     base_comercial 
    WHERE anno = DATE_FORMAT(fecha, '%Y') 
     AND mes = DATE_FORMAT(fecha, '%c') 
     AND local_formato_retail_codigo = l_local_formato_retail_codigo 
     AND producto_sku = l_producto_sku) ; 
    SET l_un_2 = 
    (SELECT 
     SUM(sellout_un) 
    FROM 
     base_comercial 
    WHERE anno = DATE_FORMAT(
     DATE_ADD(fecha, INTERVAL - 1 MONTH), 
     '%Y' 
    ) 
     AND mes = DATE_FORMAT(
     DATE_ADD(fecha, INTERVAL - 1 MONTH), 
     '%c' 
    ) 
     AND local_formato_retail_codigo = l_local_formato_retail_codigo 
     AND producto_sku = l_producto_sku) ; 
    IF (ISNULL(l_clp_1)) 
    THEN SET l_clp_1 = 0 ; 
    END IF ; 
    IF (ISNULL(l_clp_2)) 
    THEN SET l_clp_2 = 0 ; 
    END IF ; 
    IF (ISNULL(l_un_1)) 
    THEN SET l_un_1 = 0 ; 
    END IF ; 
    IF (ISNULL(l_un_2)) 
    THEN SET l_un_2 = 0 ; 
    END IF ; 
    SET l_precio = ((l_clp_1 + l_clp_2)/(l_un_1 + l_un_2)) ; 
    IF (ISNULL(l_precio)) 
    THEN SET l_precio = 0 ; 
    END IF ; 
    INSERT INTO precio_cd 
    VALUES 
     (
     l_local_formato_retail_codigo, 
     l_producto_sku, 
     l_producto_descripcion_sra, 
     l_producto_marca, 
     l_producto_categoria, 
     l_precio 
    ) ; 
    END LOOP read_loop ; 
    CLOSE valores ; 
    SELECT 
    * 
    FROM 
    precio_cd ; 
+0

我試過了,它仍然給我一個問題。我想說我的代碼有問題。不過謝謝你! – DefaultDefault

+0

我認爲你需要關閉你的循環。 END LOOP ze_loop; –

+0

仍然沒有做到。我認爲別的東西是關閉的。雖然謝謝! – DefaultDefault