Skip to Main Content
  • Questions
  • Ref cursor to bulk fetch all column values along with rowids

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Venkatesh.

Asked: June 23, 2008 - 2:17 pm UTC

Last updated: June 23, 2008 - 6:01 pm UTC

Version: 10.2.0.3

Viewed 10K+ times! This question is

You Asked

Hi Tom,

I am trying to write a procedure which would take a given table and bulk collect into 'table of rowtype' variables along with rowids (table of rowid). Something like below.

create table vtest1 (a number, b number);

1. Works fine

declare
type refCursor is ref cursor;
type rowidTab  is table of rowid;
type vtest1Tab is table of vtest1%rowtype;
rc refCursor;
v_vtest1 vtest1Tab;
v_rowidtab rowidTab;
begin
    open rc for 'select * from vtest1';
    fetch rc bulk collect into v_vtest1;
    null; ---- No actions yet.
    close rc;
end;
/


2. Does not work

declare
type refCursor is ref cursor;
type rowidTab  is table of rowid;
type vtest1Tab is table of vtest1%rowtype;
rc refCursor;
v_vtest1 vtest1Tab;
v_rowidtab rowidTab;
p_batchsize number:=500;
begin
    open rc for 'select *,rowid from vtest1';
    fetch rc bulk collect into v_vtest1,v_rowidtab limit p_batchsize;
    null; ---- No actions yet.
    close rc;
end;
/


fetch rc bulk collect into v_vtest1,v_rowidtab;
*
ERROR at line 10:
ORA-06550: line 10, column 32:
PLS-00597: expression 'V_VTEST1' in the INTO list is of wrong type
ORA-06550: line 10, column 5:
PL/SQL: SQL Statement ignored

However in (2) when I use individual columns of the table in the SELECT and corresponding variables in the INTO clause, then this works fine.

Idea is to write a generic procedure which takes any given table and bulk collects (the key is unknown until tablename is known). At some point it needs to delete the selected rows (hence rowids).

What am I missing in the second case?

Thanks Tom!

Venkatesh

and Tom said...

you either select into

a) a table of records
b) a set of tables of scalars

you cannot do both A and B


I would suggest you abandon your goal of "Idea is to write a generic procedure which takes any given table and bulk collects (the key is unknown until tablename is known). At some point it needs to delete the selected rows (hence rowids)."


I don't see the point of it. Generic code is going to be non-performing code (slow) and buggy code (hard to implement). Straight forward code is good code.

Besides, this would only work on tables that all have the SAME EXACT structure (number of columns, datatypes). If you have such a case, we need to talk about your design - for you really obviously meant to have a single table!

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks

A reader, June 23, 2008 - 4:59 pm UTC

Thanks Tom, I was suspecting something like that as it would work when all are scalars. My table structures are all different, that's where I started to look for ref cursor so this could be generic. Well, I am already abandoning the idea.

Thanks for your very prompt response!

Venkatesh
Tom Kyte
June 23, 2008 - 6:01 pm UTC

excellent :)

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library