refresh
refresh

Reputation: 1329

DBMS_SQL.TO_REFCURSOR equivalent in Oracle 10g

I have the following code :

 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);

Here I am getting the following error :

pls 00302 component 'TO_REFCURSOR' must be declared

since my oracle version is 10g.

Any idea of how to do the equivalent in Oracle 10g?

Upvotes: 0

Views: 955

Answers (1)

Boneist
Boneist

Reputation: 23588

Here's how you could use native dynamic 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;

Note that I've changed the name of the procedure since "replace" is the name of a pre-existing built-in function, and therefore not a very good name to use.

You don't mention what it is you're going to do with the results of your query, so I wasn't sure if opening a ref cursor is what you actually need, or whether bulk collecting via execute immediate would work better for you.

Upvotes: 2

Related Questions