1
respuesta

[Sugerencia] Calculando el valor total de la facturación

Buen dia, quedaria asi: cursor

CREATE DEFINER=`root`@`localhost` PROCEDURE `total_fact_mes_anio`(vmes INT, vyear year)
BEGIN
    #DECLARE query varchar(70);
    DECLARE vcantidad INT;
    DECLARE vprecio FLOAT;
    DECLARE vsuma FLOAT;
    DECLARE fin_cursor INT;
    DECLARE c CURSOR FOR 
        SELECT IFa.CANTIDAD, IFa.PRECIO FROM items_facturas IFa
        INNER JOIN facturas  F ON F.NUMERO = IFa.NUMERO
        WHERE MONTH(F.FECHA_VENTA) = vmes AND YEAR(F.FECHA_VENTA) = vyear;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET fin_cursor = 1;
    SET fin_cursor = 0;
    SET vcantidad = 0;
    SET vprecio = 0;
    SET vsuma = 0;
    OPEN c;
    WHILE fin_cursor = 0 DO
        FETCH c INTO vcantidad, vprecio;
        IF fin_cursor = 0 THEN 
            SET vsuma = vsuma + (vcantidad * vprecio );
        END IF;
    END WHILE;
    #SET query =  "CONCAT('SUMA FACTURACION '+vsuma+' MES '+vmes+' AÑO '+vyear)";
    SELECT CONCAT('SUMA FACTURACION ',vsuma,' MES ',vmes,' AÑO ',vyear) as RES;
    CLOSE c;
END

llamado o ejecucion del cursor.

#CURSOR PARA TOTAL FACTURACION MES AÑo
CALL total_fact_mes_anio(1, 2017);

Una sugerencia.... El DO del WHILE, deberia quedar en la misma linea..

WHILE fin_cursor = 0 DO
        FETCH c INTO vcantidad, vprecio;
        IF fin_cursor = 0 THEN 
            SET vsuma = vsuma + (vcantidad * vprecio );
        END IF;
    END WHILE;

En español mientas que tal condicion haga, cuerpo de sentencias del while y fin del while (mientras que) lo mismo el IF.THEN

IF fin_cursor = 0 THEN 
            SET vsuma = vsuma + (vcantidad * vprecio );
END IF;

En español SI tal condicion entonces, grupo se sentencias del IF y fin del IF. Se lee mas facil, segun mi concepto.

1 respuesta

Hola, Rene, todo bien? Excelente tu actividad colocando en práctica lo que aprendiste en la aula!

Si tienes alguna pregunta sobre el contenido de los cursos, estaremos aquí para ayudarte. Recomiendo que interactúes con el resto de tus compañeros en el Discord.

Si este post te ayudó, por favor, marca como solucionado ✓.