Db2存储过程错误(在使用它的上下文中无效)

时间:2015-05-08 02:33:55

标签: db2 db2-luw

我的问题是当我调用存储过程时它无法正常工作

TenantScriptCall.new(:name => "TEST_01", :location_id => location.id, :script_id => 12, :script_call_arguments_attributes [:script_argument_id => 16, :argumentable_id => self.id, :argumentable_type => "AstQueue", [:script_argument_id => 17, :value => "", [:script_argument_id => 18, :value => "", [:script_argument_id => 19, :argumentable_id => Playlist.last.id, :argumentable_type => "Playlist"]]]])

我的程序已成功创建但当我调用它时会发生以下问题:

  

db2“调用DELETE_WITH_COMMIT_COUNT1('TESTING',50)”SQL0206N“V1”是   在使用它的上下文中无效。 SQLSTATE = 42703

更多信息:

  

db2“从TESTING中选择min(MESSAGE_ID),max(MESSAGE_ID)   TIMESTAMP在TESTING之间(选择TIMESTAMP(日期(min(时间戳)))   用ur)和(选择TIMESTAMP(日期(分钟(时间戳)))+ 1天   用你的测试“

TenantScriptCall.new(:name => "TEST_01", :location_id => location.id, :script_id => 12, :script_call_arguments_attributes [:script_argument_id => 16, :argumentable_id => self.id, :argumentable_type => "AstQueue"] [:script_argument_id => 17, :value => ""] [:script_argument_id => 18, :value => ""] [:script_argument_id => 19, :argumentable_id => Playlist.last.id, :argumentable_type => "Playlist"])

我想删除这些值之间的记录,目前我在最小和最大消息ID之间有99条记录

drop procedure DELETE_WITH_COMMIT_COUNT1
DB20000I  The SQL command completed successfully.

CREATE PROCEDURE DELETE_WITH_COMMIT_COUNT1(IN v_TABLE_NAME VARCHAR(24), IN v_COMMIT_COUNT INTEGER )
    NOT DETERMINISTIC
    LANGUAGE SQL
P1 : BEGIN

    -- DECLARE Statements
    DECLARE SQLCODE INTEGER;
    DECLARE v_DELETE_QUERY VARCHAR(1024);
    DECLARE v_DELETE_STATEMENT STATEMENT;

P2 : BEGIN
    DECLARE V1 CHAR(24) FOR BIT DATA;
    DECLARE V2 CHAR(24) FOR BIT DATA ;

DECLARE cur1 CURSOR WITH RETURN TO CLIENT FOR select min(MESSAGE_ID),max(MESSAGE_ID) from TESTING where TIMESTAMP between (select TIMESTAMP(date(min(timestamp))) from TESTING with ur) and (select TIMESTAMP(date(min(timestamp))) + 1 day from TESTING with ur) ;

OPEN cur1;
FETCH FROM cur1 INTO V1, V2;

   SET v_DELETE_QUERY = 'DELETE FROM (SELECT 1 FROM ' || v_TABLE_NAME || ' WHERE MESSAGE_ID between V1 and V2 '
        || ' FETCH FIRST ' || RTRIM(CHAR(v_COMMIT_COUNT)) || ' ROWS ONLY) AS DELETE_TABLE';

    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 P2;
END P1
DB20000I  The SQL command completed successfully.

1 个答案:

答案 0 :(得分:0)

你的问题在于这句话:

SET v_DELETE_QUERY = 'DELETE FROM (SELECT 1 FROM ' || v_TABLE_NAME || ' WHERE   MESSAGE_ID between V1 and V2 '
        || ' FETCH FIRST ' || RTRIM(CHAR(v_COMMIT_COUNT)) || ' ROWS ONLY) AS DELETE_TABLE';

在此声明中,您似乎想要使用先前声明的变量V1和V2的值:

' WHERE   MESSAGE_ID between V1 and V2 '

DB2将此视为字符串文字。相反,尝试更改语句的这一部分,如下所示:

SET v_DELETE_QUERY = 'DELETE FROM (SELECT 1 FROM ' || v_TABLE_NAME || ' WHERE   MESSAGE_ID between ' || V1 || ' and ' || V2
            || ' FETCH FIRST ' || RTRIM(CHAR(v_COMMIT_COUNT)) || ' ROWS ONLY) AS DELETE_TABLE';