DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`packingListSwitch`(saleFk INT) BEGIN DECLARE valueFk INT; DECLARE encajando INT DEFAULT 9; SELECT isChecked INTO valueFk FROM saleTracking WHERE saleFk = saleFk AND stateFk = encajando; SET valueFk = (IFNULL(valueFk,0) + 1) MOD 3; REPLACE saleTracking(saleFk, isChecked, workerFk, stateFk) VALUES(saleFk,valueFk,account.myUser_getId(),encajando); END$$ DELIMITER ;