SQL错误:DB2 SQL错误:SQLCODE = -104,SQLSTATE = 42601,SQLERRMC = AS;仅第一行100行);),DRIVER = 3.57.82

时间:2015-08-12 09:20:00

标签: stored-procedures db2 prepared-statement

以下DB2存储过程已成功编译但收到运行时错误SQL Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=AS;FIRST 100 ROWS ONLY);), DRIVER=3.57.82

以下db2过程的意图是使用预准备语句从多个表中删除记录。

CREATE PROCEDURE ALL_DATA_PURGE1(IN v_SCHEMA_NAME VARCHAR(24),IN v_DAYS INTEGER, IN v_COMMIT_COUNT INTEGER)
    NOT DETERMINISTIC
    LANGUAGE SQL
BEGIN

    -- DECLARE Statements
    DECLARE SQLCODE INTEGER;

    DECLARE v_CRAD_DELETE_QUERY    VARCHAR(1024);
    DECLARE v_APPDATA_DELETE_QUERY VARCHAR(1024);

    DECLARE v_CRAD_DELETE_STATEMENT STATEMENT;
    DECLARE v_APPDATA_DELETE_STATEMENT STATEMENT;

    SET v_CRAD_DELETE_QUERY = 'DELETE FROM (SELECT 1 FROM ' || v_SCHEMA_NAME || '.CRAD WHERE UNIQUE_ID IN  
    (SELECT UNIQUE_ID FROM  ' || v_SCHEMA_NAME || '.APPDATA WHERE ORIGINATOR_TIMESTAMP < (
      SELECT CURRENT TIMESTAMP - '|| v_DAYS ||' DAYS FROM SYSIBM.SYSDUMMY1)
      ) FETCH FIRST ' || v_COMMIT_COUNT || ' ROWS ONLY) AS DELETE_CRAD_TABLE';      

    SET v_APPDATA_DELETE_QUERY = 'DELETE FROM (SELECT 1 FROM ' || v_SCHEMA_NAME || '.APPDATA WHERE SOME_TIMESTAMP < (
      SELECT CURRENT TIMESTAMP - ' || v_DAYS || ' days FROM sysibm.sysdummy1
      ) FETCH FIRST ' || v_COMMIT_COUNT || ' ROWS ONLY) AS DELETE_APPDATA_TABLE';


    PREPARE v_CRAD_DELETE_STATEMENT    FROM v_CRAD_DELETE_QUERY;  
    PREPARE v_APPDATA_DELETE_STATEMENT FROM v_APPDATA_DELETE_QUERY;

    DELETE_LOOP:
        LOOP
            EXECUTE v_CRAD_DELETE_STATEMENT;
            EXECUTE v_APPDATA_DELETE_STATEMENT;

            IF SQLCODE = 100 THEN
                LEAVE DELETE_LOOP; 
            END IF;
            COMMIT;
        END LOOP;

   COMMIT;

END

1 个答案:

答案 0 :(得分:1)

存储过程编译是因为动态SQL语句未知。在运行时,DB2会注意到您没有遵循其语法规则。这些陈述看起来过于复杂,可以简化。你不需要从sysdummy1中选择一些简单的表达式。

您是否在编写存储过程之前测试了各个语句?