sql—自动化db2存储过程中表的参数输入

qq24tv8q  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(378)

我使用此处显示的存储过程通过参数化表名、提交计数和where条件来删除12个大型表。我已经将这3条信息插入到一个名为 PURGE_DEL 它有12行,我想要一些建议,如何通过一个接一个地从表中读取来自动删除12个表。
像这样的
表名:purge\ U del

CALL DELETE_DATA(PURGE_DEL.TABLE_NAME, PURGE_DEL.COMMIT_COUNT, PURGE_DEL.WHERE)

代码:

CREATE PROCEDURE DELETE_DATA
(IN V_TABLE_NAME VARCHAR(24), 
IN V_COMMIT_COUNT INTEGER, 
IN V_WHERE_CONDITION VARCHAR(1024))

DYNAMIC RESULT SETS 01
COMMIT ON RETURN NO
LANGUAGE    SQL
NOT DETERMINISTIC

L3 : BEGIN

-- DECLARE STATEMENTS
DECLARE SQLCODE INTEGER;
DECLARE V_DELETE_QUERY VARCHAR(1024);
DECLARE V_DELETE_STATEMENT STATEMENT;

SET V_DELETE_QUERY = 'DELETE FROM ' || V_TABLE_NAME || 
' WHERE COLUMN IN (SELECT COLUMN FROM  ' 
|| V_TABLE_NAME ||  'WHERE COLUMN_ID =' 
|| V_WHERE_CONDITION  || ' FETCH FIRST ' 
|| RTRIM(CHAR(V_COMMIT_COUNT)) || ' ROWS ONLY) ';

PREPARE V_DELETE_STATEMENT FROM V_DELETE_QUERY;
DEL_LOOP:
    LOOP
        EXECUTE V_DELETE_STATEMENT;
        IF SQLCODE = 100 THEN
            LEAVE DEL_LOOP; 
        END IF;
        COMMIT;
    END LOOP;

COMMIT;
END L3

还请建议对存储过程进行任何更改。

inkz8wg9

inkz8wg91#

出于兴趣,您可以使用这种类型的delete来避免delete中代价高昂的自连接

DELETE FROM (
    SELECT 1 FROM TABLE_NAME
    WHERE WHERE_CONDITION  
    FETCH FIRST 50000 ROWS ONLY
)

而且,如果您确实希望高效地清除行,可以考虑使用mdc卷展栏删除
https://www.ibm.com/support/knowledgecenter/ssepgg_11.5.0/com.ibm.db2.luw.admin.perf.doc/doc/c0007338.html

相关问题