使用DBMS_SQL.Execute_and_fetch - 获取Ref游标的光标号(10g)

时间:2014-05-07 09:09:24

标签: oracle plsql oracle10g

使用Oracle 10g DBMS,我想在过程中应用DBMS_SQL.execute_and_fetch。我拒绝EXECUTE IMMEDIATE因为我想绑定命名变量而不依赖于它们在语句中的顺序。

问题是我无法处理传递给我的过程的olist引用游标参数的游标号。以下是我正在尝试做的示例代码片段。

PROCEDURE TestProc( vuserid INTEGER,
                    olist   OUT refcur)
IS
  cursor_num INTEGER;
  vsql       VARCHAR2(1000 CHAR);
BEGIN
  vsql := 'SELECT * FROM USERS WHERE USER_ID = :user_id';

  /*creates a new cursor. Need to use the cursor that is passed to my procedure*/
  /*cursor_num := dbms_sql.open_cursor; */

  /*Only available in 11g onwards        */
  /*cursor_num := dbms_sql.to_cursor_number(olist); */

  sys.dbms_sql.parse(cursor_num, vsql, sys.dbms_sql.native);
  sys.dbms_sql.bind_variable(cursor_num, ':user_id', vuserid);
  sys.dbms_sql.execute_and_fetch(cursor_num, FALSE);
END TestProc;

如何在我的特定情况下成功使用DBMS_SQL.execute_and_fetch过程,其中用于获取的游标是我传入的引用游标?

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找这个:

PROCEDURE TestProc (
      vUserid   INTEGER,
      olist              OUT   SYS_REFCURSOR)
IS
   cursor_num INTEGER;
   vSQL VARCHAR2(1000 CHAR);
    result NUMBER;
BEGIN   
    vSQL := 'SELECT * FROM USERS WHERE USER_ID = :user_id';

    cursor_num := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE(cursor_num,vSQL,DBMS_SQL.NATIVE);
    DBMS_SQL.BIND_VARIABLE(cursor_num, ':user_id', vUserid);
    result := DBMS_SQL.EXECUTE(cursor_num);
    olist := DBMS_SQL.TO_REFCURSOR(cursor_num);

END TestProc;

然后,调用块可以使用FETCH olist INTO...FETCH olist BULK COLLECT INTO...来获取查询结果。

但是,在它不可用之前,你必须至少运行Oracle 11。