can TYPE be declared of ref cursor rowtype

HalfWebDev picture HalfWebDev · Aug 1, 2013 · Viewed 10.9k times · Source
 TYPE ref_cur IS REF CURSOR;
 ref_cur_name    ref_cur;
 TYPE tmptbl IS  TABLE OF ref_cur_name%ROWTYPE;
 n_tmptbl        tmptbl;

I tried this code but can't get it thru compiler . Is there a way to store the results of ref cursor into a table ?

NOTE-I need a table because i need to access the column of ref cursor . Using dbms_sql to access records of ref cursor is a bit tough for me .

UPDATE :

/* Formatted on 8/1/2013 4:09:08 PM (QP5 v5.115.810.9015) */
CREATE OR REPLACE PROCEDURE proc_deduplicate (p_tblname   IN VARCHAR2,
                                               p_cname     IN VARCHAR2,
                                               p_cvalue    IN VARCHAR2)
IS
   v_cnt          NUMBER;

   TYPE ref_cur IS REF CURSOR;
   ref_cur_name   ref_cur;


   v_str1         VARCHAR2 (4000);
   v_str2         VARCHAR2 (4000);
   v_str3         VARCHAR2 (4000);
BEGIN
   v_str1 :=
         'SELECT ROWID v_rowid FROM '
      || p_tblname
      || ' WHERE '
      || p_cname
      || '='''
      || p_cvalue
      || '''';


   BEGIN
      v_str2 :=
            'SELECT   COUNT ( * )

         FROM  '
         || p_tblname
         || ' WHERE  '
         || p_cname
         || ' = '''
         || p_cvalue
         || '''';
      logerrors ('proc_deduplicate',
                 'count exception',
                 SQLCODE,
                 v_str2 || SQLERRM,
                 'e');

      EXECUTE IMMEDIATE v_str2 INTO   v_cnt;
   EXCEPTION
      WHEN OTHERS
      THEN
         logerrors ('proc_deduplicate',
                    'count exception',
                    SQLCODE,
                    SQLERRM,
                    'e');
   END;

   IF v_cnt IS NOT NULL
   THEN
     OPEN ref_cur_name FOR v_str1;

      LOOP
         IF v_cnt = 1
         THEN
            EXIT;
         ELSE
            BEGIN
               v_str3 :=
                     'DELETE FROM '
                  || p_tblname
                  || ' WHERE   ROWID = v_rowid '; 
 -- THIS IS THE PROBLEM . i just created an alias above for rowid keyword but i guess, DBMS sql will have to be used  after all . 


               EXECUTE IMMEDIATE v_str3;
            EXCEPTION
               WHEN OTHERS
               THEN
                  logerrors (
                     '                                                            proc_deduplicate
      ',
                     '                                                            delete exception
      ',
                     SQLCODE,
                     SQLERRM,
                     '                                                            e
      '
                  );
            END;
         END IF;

         v_cnt := v_cnt - 1;
      END LOOP;
   END IF;
EXCEPTION
   WHEN OTHERS
   THEN
      logerrors (
         '                                    proc_deduplicate',
         '                                    final exception
      ',
         SQLCODE,
         SQLERRM,
         '                                    e'
      );
END;
/

Answer

Nick Krasnov picture Nick Krasnov · Aug 1, 2013

By issuing TYPE ref_cur IS REF CURSOR you are declaring a weak cursor. Weak cursors return no specified types. It means that you cannot declare a variable that is of weak_cursor%rowtype, simply because a weak cursor does not return any type.

declare
  type  t_rf  is ref cursor;
  l_rf  t_rf;
  type  t_trf is table of l_rf%rowtype;
  l_trf t_trf;
begin
  null;
end;

ORA-06550: line 4, column 27:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 4, column 3:
PL/SQL: Item ignored    

If you specify return type for your ref cursor, making it strong, your PL/SQL block will compile successfully:

 SQL> declare                            -- strong cursor
  2    type  t_rf  is ref cursor return [table_name%rowtype][structure]; 
  3    l_rf  t_rf;                                         
  4    type  t_trf is table of l_rf%rowtype;
  5    l_trf t_trf;
  6  begin
  7    null;
  8  end;
  9  /

PL/SQL procedure successfully completed