If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Using Cursor with for... In......end loop syntax

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-29-03, 17:38
pdt pdt is offline
Registered User
 
Join Date: Nov 2002
Posts: 8
Question Using Cursor with for... In......end loop syntax

If I use a cursor with the for... IN ...end loop syntax....like


for rec_base in cInterval
if rec_base.trans_id = blah blah blah...
end loop

will the cursor close automatically after it finds it's last row. I would like
to use the same cursor a number of times in a single procedure. Each time I use it I need the cursor to run the select again to make sure
it's result set is current.


Thank you
Reply With Quote
  #2 (permalink)  
Old 01-30-03, 01:41
NoviceNo1 NoviceNo1 is offline
Registered User
 
Join Date: Jan 2003
Location: Woking
Posts: 107
Re: Using Cursor with for... In......end loop syntax

Quote:
Originally posted by pdt
If I use a cursor with the for... IN ...end loop syntax....like


for rec_base in cInterval
if rec_base.trans_id = blah blah blah...
end loop

will the cursor close automatically after it finds it's last row. I would like
to use the same cursor a number of times in a single procedure. Each time I use it I need the cursor to run the select again to make sure
it's result set is current.


Thank you
Hi,
Use a ref cursor for this and give
open c1 for <select stmt> each time. this will give you the
latest data.
__________________
nn
Reply With Quote
  #3 (permalink)  
Old 01-30-03, 06:32
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: Using Cursor with for... In......end loop syntax

Quote:
Originally posted by pdt
If I use a cursor with the for... IN ...end loop syntax....like


for rec_base in cInterval
if rec_base.trans_id = blah blah blah...
end loop

will the cursor close automatically after it finds it's last row. I would like
to use the same cursor a number of times in a single procedure. Each time I use it I need the cursor to run the select again to make sure
it's result set is current.


Thank you
Yes, the cursor will be closed automatically after the last row is fetched, or if you exit from the loop prematurely for any reason. And each time you open the cursor it will get the current data. So you have no problems with this approach.

My only comment would be that if your example above was the requirement, it would be more efficient if possible to code:

DECLARE
cursor cInterval is
select .... from ... where ...
and rec_base.trans_id = blah blah blah...
BEGIN
for rec_base in cInterval
...
end loop;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #4 (permalink)  
Old 01-30-03, 12:33
pdt pdt is offline
Registered User
 
Join Date: Nov 2002
Posts: 8
Re: Using Cursor with for... In......end loop syntax

Quote:
Originally posted by andrewst
Yes, the cursor will be closed automatically after the last row is fetched, or if you exit from the loop prematurely for any reason. And each time you open the cursor it will get the current data. So you have no problems with this approach.

My only comment would be that if your example above was the requirement, it would be more efficient if possible to code:

DECLARE
cursor cInterval is
select .... from ... where ...
and rec_base.trans_id = blah blah blah...
BEGIN
for rec_base in cInterval
...
end loop;
Thank you for the answers. Nice to know.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On