虽然循环不能按预期在mysql PROCEDURE中工作

时间:2015-08-14 12:34:11

标签: mysql stored-procedures

tbl_master_sales中有100行,空表tbl_customer_sales。当我使用WHILE循环将数据从tbl_master_sales插入tbl_customer_sales时,它只插入50行。但是,它应该插入100行,并进行两次while循环迭代。在下面的程序中可能是我的错误:

CREATE PROCEDURE ROWPERROW()
BEGIN
    DECLARE n INT DEFAULT 0;
    DECLARE i INT DEFAULT 0;
    SELECT COUNT(*) FROM tbl_master_sales INTO n;
    SET i=0;
    WHILE i<n DO 

        INSERT INTO tbl_customer_sales (id,card_number,customer_name,customer_phone,bill_no,item_code,division,section,department,item_name,store,promo_name,billdiscount_name,billqty,promo_amount,bill_discount_amount,loyaltyamount,net_amount) 
            SELECT id, card_number, customer_name, customer_mobile, billno, itemcode, division, section, department, itemname, store, promoname, billdiscountname, billqty, promoamount, billdiscountamount, loyaltyamount, netamount
              FROM tbl_master_sales
             WHERE NOT EXISTS(SELECT 1 
                                FROM tbl_customer_sales 
                               WHERE id=tbl_master_sales.id) 
             LIMIT i,50;


        SET i = i + 50;
    END WHILE;
End;;

1 个答案:

答案 0 :(得分:0)

我没有看到您的过程代码逻辑有任何问题,但是只插入50行的原因可能是下面显示的NOT EXISTS部分,这限制了插入重复行(或)过滤掉休息记录。

WHERE NOT EXISTS(
SELECT 1 
FROM tbl_customer_sales 
WHERE id=tbl_master_sales.id)