如何使用游标递归删除记录

时间:2015-04-03 19:55:25

标签: mysql sql stored-procedures db2

CREATE OR REPLACE PROCEDURE  WRK.PURGE_ACH_BATCH_TAB_1()
LANGUAGE SQL

BEGIN
    -- Declare cursor
    DECLARE v_CNT_BLOCK INTEGER;
    DECLARE Ac_no BIGINT;
    DECLARE SQLSTATE CHAR(5) DEFAULT '00000'; 
    DECLARE mycur cursor for 
        SELECT t.TNX_Ac_no
        FROM WRK.BATCH_TAB1 c
            INNER JOIN WRK.BATCH_TAB t
                ON c.BATCH_ID = t.BATCH_ID
                    AND c.PROCESS_ID = t.process_ID
        WHERE c.BATCH_START_TS < (CURRENT DATE -7 days); 

    set v_CNT_BLOCK = 0;
    open mycur;

    FETCH FROM mycur INTO Ac_no;
    WHILE(SQLSTATE = '00000') DO
        DELETE FROM WRK.BATCH_TAB1 b WHERE b.TNX_Ac_no = Ac_no;
        SET v_CNT_BLOCK=v_CNT_BLOCK+1;
        if v_CNT_BLOCK >= 5 then
            set v_CNT_BLOCK = 0;
            commit;
        end if;

        FETCH FROM mycur INTO Ac_no;  

    END WHILE; 

    CLOSE mycur; 

    commit; 

END
@ 

我有数百万条记录,但我正在获取所有记录。后来我逐一取出删除。我的问题是,一次选择所有数百万条记录会导致任何性能问题。如何选择记录recursivEly,以便我可以先取50k记录并删除所有.. Latar next 50k等等..

0 个答案:

没有答案