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

41 lines
957 B
SQL

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