Oracle 10g中的DBMS_SQL.TO_REFCURSOR等价物

时间:2017-06-15 06:45:53

标签: oracle plsql oracle10g dynamic-sql

我有以下代码:

 procedure Replace(sUser in Varchar2,sNomTable in varchar2,sColonne in varchar2,sID in Varchar2,nbCharAlterer IN NUMBER)  is

   l_cursor NUMBER;
   l_return NUMBER;
   l_ref_cursor SYS_REFCURSOR;
   TYPE t_tab IS  TABLE OF VARCHAR2(4000);
   l_tab t_tab;
   l_tab_Id t_tab;
   sChaine VARCHAR2(4000 CHAR);
   sqlReq CONSTANT VARCHAR2(1000):= 'select  ' || sId || ',' ||  sColonne || ' from ' || sUser || '.' || sNomTable  ;

begin
   --
   l_cursor := DBMS_SQL.open_cursor;
   DBMS_SQL.parse(l_cursor, sqlReq, DBMS_SQL.NATIVE);
   l_return := DBMS_SQL.EXECUTE(l_cursor);
   -- Connvert from DBMS_SQL to a REF CURSOR.
   l_ref_cursor := DBMS_SQL.to_refcursor(l_cursor);

这里我收到以下错误:

pls 00302 component 'TO_REFCURSOR' must be declared

因为我的oracle版本是10g。

如何在Oracle 10g中完成等效操作?

1 个答案:

答案 0 :(得分:2)

以下是如何使用原生动态sql:

PROCEDURE p_replace(suser         IN VARCHAR2,
                    snomtable     IN VARCHAR2,
                    scolonne      IN VARCHAR2,
                    sid           IN VARCHAR2,
                    nbcharalterer IN NUMBER) IS
  v_validate_sid_col_name VARCHAR2(32);
  v_validate_scolonne_col_name VARCHAR2(32);
  v_validate_suser VARCHAR2(32);
  v_validate_snomtable VARCHAR2(32);

  sqlreq VARCHAR2(2000);

  refcur sys_refcur;
BEGIN
  -- Check the input values are valid identifiers (to avoid sql injection)
  -- N.B. this does not check they are valid object names!
  v_validate_sid_col_name := dbms_assert.qualified_sql_name(sid);
  v_validate_scolonne_col_name := dbms_assert.qualified_sql_name(scolonne);
  v_validate_suser := dbms_assert.qualified_sql_name(suser);
  v_validate_snomtable := dbms_assert.qualified_sql_name(scolonne);

  sqlReq := 'select  ' || v_validate_sid_col_name || ',' ||
                          v_validate_scolonne_col_name ||
            ' from ' || v_validate_suser || '.' || v_validate_snomtable;

  -- or maybe you want to use execute immediate to bulk collect into arrays?
  OPEN refcur FOR sqlreq;

  ...
END p_replace;

请注意,我已更改过程的名称,因为"替换"是预先存在的内置函数的名称,因此不是一个非常好用的名称。

你没有提到你的查询结果是什么,所以我不确定打开引用光标是你真正需要的,还是批量通过执行立即收集将更好地为您工作。