dbTalk Databases Forums  

Looping through column values in a record without specifying columnnames?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Looping through column values in a record without specifying columnnames? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
dananrg@yahoo.com
 
Posts: n/a

Default Looping through column values in a record without specifying columnnames? - 03-04-2008 , 09:38 AM






In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

for x in rec
loop
dbms_output.put_line(x)
end loop;

.... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana




Reply With Quote
  #2  
Old   
Thomas Olszewicki
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-04-2008 , 02:56 PM






On Mar 4, 10:38*am, dana... (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
* * *dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

* * * * * * *for x in rec
* * * * * * *loop
* * * * * * * * *dbms_output.put_line(x)
* * * * * * *end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
I know only one such method to use.
Use dynamic sql with DBMS_SQL package.
You can retrieve column names using procedure describe_columns.
HTH
Thomas


Reply With Quote
  #3  
Old   
Thomas Olszewicki
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-04-2008 , 02:56 PM



On Mar 4, 10:38*am, dana... (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
* * *dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

* * * * * * *for x in rec
* * * * * * *loop
* * * * * * * * *dbms_output.put_line(x)
* * * * * * *end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
I know only one such method to use.
Use dynamic sql with DBMS_SQL package.
You can retrieve column names using procedure describe_columns.
HTH
Thomas


Reply With Quote
  #4  
Old   
Thomas Olszewicki
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-04-2008 , 02:56 PM



On Mar 4, 10:38*am, dana... (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
* * *dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

* * * * * * *for x in rec
* * * * * * *loop
* * * * * * * * *dbms_output.put_line(x)
* * * * * * *end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
I know only one such method to use.
Use dynamic sql with DBMS_SQL package.
You can retrieve column names using procedure describe_columns.
HTH
Thomas


Reply With Quote
  #5  
Old   
Thomas Olszewicki
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-04-2008 , 02:56 PM



On Mar 4, 10:38*am, dana... (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
* * *dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

* * * * * * *for x in rec
* * * * * * *loop
* * * * * * * * *dbms_output.put_line(x)
* * * * * * *end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
I know only one such method to use.
Use dynamic sql with DBMS_SQL package.
You can retrieve column names using procedure describe_columns.
HTH
Thomas


Reply With Quote
  #6  
Old   
DA Morgan
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-05-2008 , 06:51 AM



dananrg (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

for x in rec
loop
dbms_output.put_line(x)
end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
Cursor loops such as in your example have been obsolete since the
introduction of Oracle 9i.

BULK COLLECT into an array indexed by binary integer and you can do this
with ease (Morgan's Library at www.psoug.org).
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #7  
Old   
DA Morgan
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-05-2008 , 06:51 AM



dananrg (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

for x in rec
loop
dbms_output.put_line(x)
end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
Cursor loops such as in your example have been obsolete since the
introduction of Oracle 9i.

BULK COLLECT into an array indexed by binary integer and you can do this
with ease (Morgan's Library at www.psoug.org).
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #8  
Old   
DA Morgan
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-05-2008 , 06:51 AM



dananrg (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

for x in rec
loop
dbms_output.put_line(x)
end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
Cursor loops such as in your example have been obsolete since the
introduction of Oracle 9i.

BULK COLLECT into an array indexed by binary integer and you can do this
with ease (Morgan's Library at www.psoug.org).
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #9  
Old   
DA Morgan
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-05-2008 , 06:51 AM



dananrg (AT) yahoo (DOT) com wrote:
Quote:
In PL/SQL, is there a way to loop through column values in a record
without specifying column names in advance? e.g. like using an index
number in an array?

The code below will output a value for the column specified; but I've
not found a way to create an inner loop and iterate through the record
itself.

DECLARE

cursor cur is
select * from my_table;

BEGIN

for rec in cur
loop
dbms_output.put_line(rec.my_column);
end loop;

END;

Tried inserting an inner loop something like the following, and it
failed:

for x in rec
loop
dbms_output.put_line(x)
end loop;

... and it failed:

ERROR at line 7:
ORA-06550: line 7, column 12:
PLS-00456: item 'REC' is not a cursor
ORA-06550: line 7, column 3:
PL/SQL: Statement ignored

So I can't use a RECORD in a FOR IN loop. How can I accomplish what I
want to do some other way?

Thanks.

Dana
Cursor loops such as in your example have been obsolete since the
introduction of Oracle 9i.

BULK COLLECT into an array indexed by binary integer and you can do this
with ease (Morgan's Library at www.psoug.org).
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #10  
Old   
dananrg@yahoo.com
 
Posts: n/a

Default Re: Looping through column values in a record without specifyingcolumn names? - 03-05-2008 , 07:58 AM



Quote:
BULK COLLECT into an array indexed by binary integer and you can do this
with ease (Morgan's Library atwww.psoug.org).
Thanks Daniel and Thomas. Is there a specific example at:
http://www.psoug.org/reference/array_processing.html
which will do what I want?

Tweaked and tried one example and it failed. Many other of the
examples on the page do a SELECT single_column rather than a SELECT *.

Also, I'm still at 9i R2. So I would need an example that works at
this release. Most of your examples are classified as 10g or 11i.

Dana


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.