Declare
var_cnt number(3):=0;
begin
loop
update t_loan_dtl set loan_closure = 'Y' where rownum <10001;
end loop;
end;
Asked
Active
Viewed 4.4k times
6

Rahul Tripathi
- 168,305
- 31
- 280
- 331

Sundararaj Thathan
- 81
- 1
- 1
- 4
-
Have any of the answers help you solve your problem. If so please accept that answer. If you came-up with a different solution then post it and accept it. If your issue is not resolved update the question and indicate the remaining issue. Point being do not just leave a resolved question in an open state. Answered questions help future questioners having the same issue. – Belayer Mar 06 '21 at 01:59
5 Answers
9
simple exit
loop --do something; exit; end loop;
conditional exit
loop --do something; exit when "condition"; end loop;
3.Exit with cursor variable
exit when v_cursor%notfound;

vishnu sable
- 328
- 1
- 7
5
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.

Rahul Tripathi
- 168,305
- 31
- 280
- 331
3
loop
if(var_cnt>=10001) then
exit;
end if;
var_cnt:=var_cnt+1;
end loop;

Nur.B
- 319
- 2
- 4
-
3Please 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. – Tyler2P Feb 25 '21 at 10:35
-
The code is very simple. When the variable reaches 10000, the loop exits – Nur.B Feb 25 '21 at 14:14
0
You can use EXIT
in Loop
I have used employees
as a Table
in the below example and Cursor
for the operation.
DECLARE
v_employees employees%ROWTYPE; -- declare record variable
CURSOR c1 is SELECT * FROM employees;
BEGIN
OPEN c1; -- open the cursor before fetching
-- An entire row is fetched into the v_employees record
FOR i IN 1..10 LOOP
FETCH c1 INTO v_employees;
EXIT WHEN c1%NOTFOUND;
-- process data here
END LOOP;
CLOSE c1;
END;
/

CandleCoder
- 1,387
- 4
- 21
- 45
-
1What is `employees`? OP has not mentioned any such table? And why a cursor? – Rahul Tripathi Feb 10 '16 at 04:45
-
-
Then you need to explicitly mention that in your answer. Else it becomes very confusing as to what your intent is! – Rahul Tripathi Feb 10 '16 at 04:51
-1
It entirely depends on when you want to exit the loop. If you want to run this only once, then there is no need of the loop statement, If you want to run say 100 times, then increment the counter and add an if condition to exit when the count is reached.

Chandru
- 11
- 4