为什么我只在最后一条记录中插入重复记录?

时间:2011-12-20 16:40:27

标签: mysql stored-procedures cursor

我从我的程序中获得了一条重复记录,其中插入了330多条记录。 但仅限于最后一次记录。换句话说,最后两个记录并不明显,它们是相同的。是什么让这个程序允许最后一条记录重复。

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 ;

2 个答案:

答案 0 :(得分:2)

这里真的不需要光标。这可以在单个INSERT语句中完成。

INSERT INTO zz_CATALOG 
    (TYPE, ENTITY_ID, ESTRICTION_TYPE, RESTRICTION_VALUE)
    SELECT DISTINCT 'Product', EDPNO, 'Country', 'ALL'
        FROM stage_product_data.ITEMMAST AS IM
            JOIN stage_product_data.zzLive_Products AS LIVE 
                ON IM.EDPNO = LIVE.SKU
        WHERE DIVISION = '30' 
            AND STATUS NOT IN ('XX','YY','ZZ');

答案 1 :(得分:0)

您确定这是您插入的数据中的过程而不是重复吗?