I get a duplicate record from my procedure which inserts 330+ records.
But ONLY on the very last record. So in other words the last 2 records are not distinct, they are the same. What is it about this procedure that allows the last record to get duplicated.
DELIMITER $$
DROP PROCEDURE IF EXISTS `zzExclude_Products` $$
CREATE DEFINER=`root`@`%` PROCEDURE `zzExclude_Products`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE VAR_ENTITY_ID VARCHAR(50);
DECLARE CUR_NO CURSOR FOR
SELECT DISTINCT NO
FROM stage_product_data.ITEMMAST AS IM
JOIN stage_product_data.zzLive_Products AS LIVE ON IM.NO = LIVE.SKU
WHERE DIVISION = '30' AND STATUS NOT IN ('XX','YY','ZZ');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN CUR_NO;
REPEAT
FETCH CUR_NO INTO VAR_ENTITY_ID;
INSERT INTO zz_CATALOG (TYPE, ENTITY_ID, ESTRICTION_TYPE, RESTRICTION_VALUE)
VALUES ('Product', VAR_ENTITY_ID, 'Country', 'ALL');
UNTIL done END REPEAT;
CLOSE CUR_NO;
END $$
DELIMITER ;
There’s really no need for a cursor here. This can be done in a single INSERT statement.