How to exit the loop in oracle

25,944

Solution 1

  1. simple exit

    loop --do something; exit; end loop;

  2. conditional exit

    loop --do something; exit when "condition"; end loop;

3.Exit with cursor variable

 exit when v_cursor%notfound;

Solution 2

You can try to use the EXIT statament

The EXIT statement breaks out of a loop. The EXIT statement has two forms: the unconditional EXIT and the conditional EXIT WHEN. With either form, you can name the loop to be exited.

Solution 3

loop
 if(var_cnt>=10001) then
  exit;
 end if;
 var_cnt:=var_cnt+1;
end loop;
Share:
25,944
Sundararaj Thathan
Author by

Sundararaj Thathan

Updated on July 09, 2022

Comments

  • Sundararaj Thathan
    Sundararaj Thathan almost 2 years
    Declare
    var_cnt       number(3):=0;
    begin
        loop
            update t_loan_dtl set loan_closure = 'Y' where rownum <10001;
        end loop;
    end;
    
  • Rahul Tripathi
    Rahul Tripathi over 8 years
    What is employees? OP has not mentioned any such table? And why a cursor?
  • CandleCoder
    CandleCoder over 8 years
    I have just given an Example here that How to Exit from a loop
  • Rahul Tripathi
    Rahul Tripathi over 8 years
    Then you need to explicitly mention that in your answer. Else it becomes very confusing as to what your intent is!
  • Tyler2P
    Tyler2P over 3 years
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
  • Nur.B
    Nur.B over 3 years
    The code is very simple. When the variable reaches 10000, the loop exits
  • FrenkyB
    FrenkyB about 2 years
    He asked how to exit loop explicitly, with condition met.