473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PL/SQL How to user "continue" in a FOR LOOP

Hi,
I am trying to skip a record if some conditions match in a for loop, what is the exact keyword to use that in PL/SQL.

e.g,
Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3. ..
  4. i NUMBER;
  5. BEGIN
  6.  
  7. FOR i <10
  8. LOOP
  9.  
  10.     IF i = 5
  11. -- I want to skip the update statement if i is equal to 5 in this example
  12.         "Continue"
  13.     END IF
  14.  
  15. UPDATE ABC ....;
  16.  
  17. END LOOP;
  18.  
Sep 25 '06 #1
5 53010
debasisdas
8,127 Expert 4TB
With the continue statement you can skip to the next iteration in a loop.

try to use the following sample code

Expand|Select|Wrap|Line Numbers
  1. begin  
  2. for i in 1..3  loop    
  3. dbms_output.put_line(’i=’||to_char(i));    
  4. if ( i = 2 )  then       
  5. continue;     
  6. end if;    
  7. dbms_output.put_line(’ Only if i is not equal to 2 ′);  
  8. end loop;
  9. end;
  10.  
Note: this is a feature of Oracle 11g . So try in that version only.
Dec 24 '07 #2
debasisdas
8,127 Expert 4TB
You can also use the continue when statement to determine when to go to the next iteration in the loop.
Expand|Select|Wrap|Line Numbers
  1. begin  
  2. for i in 1..3  loop    
  3. dbms_output.put_line(’i=’||to_char(i));    
  4. continue when ( i = 2 );    
  5. dbms_output.put_line(’Only if i is not equal to 2′);  
  6. end loop;
  7. end;
Dec 24 '07 #3
amitpatel66
2,367 Expert 2GB
Hi,
I am trying to skip a record if some conditions match in a for loop, what is the exact keyword to use that in PL/SQL.

e.g,

DECLARE
..
i NUMBER;
BEGIN

FOR i <10
LOOP

IF i = 5
-- I want to skip the update statement if i is equal to 5 in this example
"Continue"
END IF

UPDATE ABC ....;

END LOOP;
In case if you want to try out in lower versions of oracle, then try this:

Expand|Select|Wrap|Line Numbers
  1. declare
  2. begin
  3. for i in 1..10 loop
  4. declare
  5. my_execp EXCEPTION;
  6. BEGIN
  7. IF(I = 5) THEN
  8. RAISE my_exscep;
  9. ELSE
  10. UPDATE ABC....;
  11. END IF;
  12. EXCEPTION
  13. WHEN my_excep THEN
  14. dbms_output.put_line('dont update now!!');
  15. END;
  16. END LOOP;
  17. END;
  18.  
I have not tested this code. I hope this should help you out to achive your requirement!!
Dec 24 '07 #4
Hi,
I am trying to skip a record if some conditions match in a for loop, what is the exact keyword to use that in PL/SQL.

e.g,
Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3. ..
  4. i NUMBER;
  5. BEGIN
  6.  
  7. END LOOP;
  8.  
Have not used Continue, but the following works

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3. ..
  4. i NUMBER;
  5. BEGIN
  6.  
  7. FOR i <10
  8. LOOP
  9.     IF i = 5
  10.      UPDATE ABC ....;
  11.     END IF
  12. END LOOP;
  13.  
Jan 12 '08 #5
Dave44
153 100+
Have not used Continue, but the following works

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3. ..
  4. i NUMBER;
  5. BEGIN
  6.  
  7. FOR i <10
  8. LOOP
  9.     IF i = 5
  10.      UPDATE ABC ....;
  11.     END IF
  12. END LOOP;
  13.  
yep i agree with you completely, keep it simple. except in this case i think he said no update if its 5 so we'd need IF i <> 5 then
Jan 12 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Harman Sahni | last post by:
As per this URL http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true conitnue works on for, while, do... I know it works for foreach as well as I'm using it somewhere. My...
14
by: Daniel Bass | last post by:
is there an equivalent key word for C++'s "continue" in VB (.net) in this context? CString szLine; szLine = myReader.ReadLine(); while ( !szLine.IsEmpty() ) { if ( szLine(0) == '-' ) {
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
36
by: mdh | last post by:
May I ask the group this somewhat non-focused question....having now seen "continue" used in some of the solutions I have worked on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind) Is there a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.