在IBM DB2中的存储过程内执行动态选择查询时出错

时间:2018-11-05 08:17:55

标签: sql stored-procedures plsql db2 dynamicquery

以下代码有什么问题?我试图在DB2中的存储过程内执行动态查询。我的程序正在创建

CREATE PROCEDURE  get_users( in offsetValue integer,
                              in sortCol varchar(20)            
                            )
  SPECIFIC get_users
  LANGUAGE SQL 

  Re:  BEGIN

    DECLARE SELECT_STATEMENT VARCHAR(8000);
    DECLARE SQL_STATEMENT STATEMENT;

    DECLARE c_emp CURSOR FOR SQL_STATEMENT;

    set SELECT_STATEMENT = 'select * from users;';

    PREPARE SQL_STATEMENT FROM SELECT_STATEMENT;

    EXECUTE SQL_STATEMENT; 

    open c_emp;
  END Re

但是当我使用

调用过程时
call get_users(3, 'email_id');

我遇到以下错误

SQL Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;select * from users;END-OF-STATEMENT, DRIVER=4.24.92

怎么了?

1 个答案:

答案 0 :(得分:2)

错误太多... 试试这个:

--#SET TERMINATOR @

CREATE OR REPLACE PROCEDURE  get_users( in offsetValue integer,
                              in sortCol varchar(20)            
                            )
  SPECIFIC get_users
  DYNAMIC RESULT SETS 1
  LANGUAGE SQL 

  Re:  BEGIN
    DECLARE SELECT_STATEMENT VARCHAR(8000);
    DECLARE SQL_STATEMENT STATEMENT;
    DECLARE c_emp CURSOR WITH RETURN FOR SQL_STATEMENT;

    set SELECT_STATEMENT = 'select * from users';
    PREPARE SQL_STATEMENT FROM SELECT_STATEMENT;
    open c_emp;
  END Re
  @