salix/db/routines/vn/procedures/sale_setProblem.sql

41 lines
955 B
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`sale_setProblem`(
vProblemCode VARCHAR(25)
)
BEGIN
/**
* Update column sale.problem with a problem code
2024-07-12 06:15:09 +00:00
*
* @param vProblemCode Code to set or unset
* @table tmp.sale(saleFk, hasProblem, isProblemCalcNeeded)
*/
2024-07-12 06:15:09 +00:00
DECLARE vSaleFk INT;
DECLARE vHasProblem INT;
DECLARE vIsProblemCalcNeeded BOOL;
2024-07-12 06:15:09 +00:00
DECLARE vDone BOOL;
DECLARE vSaleList CURSOR FOR
SELECT saleFk, hasProblem, isProblemCalcNeeded
FROM tmp.sale;
2024-07-12 06:15:09 +00:00
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
2024-07-12 06:15:09 +00:00
OPEN vSaleList;
l: LOOP
SET vDone = FALSE;
FETCH vSaleList INTO vSaleFk, vHasProblem, vIsProblemCalcNeeded;
2024-07-12 06:15:09 +00:00
IF vDone THEN
LEAVE l;
END IF;
UPDATE sale
SET problem = IF (vIsProblemCalcNeeded,
CONCAT(
IF(vHasProblem,
CONCAT(problem, ',', vProblemCode),
REPLACE(problem, vProblemCode , ''))),
NULL)
2024-07-12 06:15:09 +00:00
WHERE id = vSaleFk;
END LOOP;
CLOSE vSaleList;
END$$
DELIMITER ;