DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expeditionScan_Add`(vPalletFk INT, vTruckFk INT) BEGIN DECLARE vTotal INT DEFAULT 0; IF vPalletFk = 0 THEN -- Nuevo pallet INSERT INTO vn.expeditionPallet(truckFk) VALUES(vTruckFk); SELECT LAST_INSERT_ID() INTO vPalletFk; END IF; SELECT COUNT(expeditionFk) INTO vTotal FROM vn.expeditionScan WHERE palletFk = vPalletFk; IF vTotal <> 0 THEN SELECT expeditionFk, palletFk FROM vn.expeditionScan WHERE palletFk = vPalletFk; ELSE SELECT 0 expeditionFk,vPalletFk palletFk; END IF; END$$ DELIMITER ;