DROP PROCEDURE IF EXISTS `vn`.`ticketRefund_beforeUpsert`; DELIMITER $$ $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticketRefund_beforeUpsert`(vRefundTicketFk INT, vOriginalTicketFk INT) BEGIN DECLARE vAlreadyExists BOOLEAN DEFAULT FALSE; IF vRefundTicketFk = vOriginalTicketFk THEN CALL util.throw('Original ticket and refund ticket has same id'); END IF; SELECT COUNT(*) INTO vAlreadyExists FROM ticketRefund WHERE originalTicketFk = vOriginalTicketFk; IF vAlreadyExists > 0 THEN CALL util.throw('This ticket is already a refund'); END IF; END$$ DELIMITER ;