Detailed usage of three loop statements in oracle stored procedure

When the program needs to perform an operation repeatedly, it must use a loop structure. The loop statement in PL/SQL mainly includes three kinds of LOOP statement, WHERE statement and FOR statement. This blog mainly explains these three usages and code examples.

This blog mainly focus on basic grammar, simple code examples, as well as the actual common example , to explain these three points, learning cycle to see this one enough.

LOOP statement

The LOOP statement will execute the loop body once, and then determine whether the conditional expression after the EXIT WHEN keyword is true or false. When it is true, it exits the loop body, otherwise the program will execute the loop body again.

Basic syntax:

loop
    A;
    EXIT WHEN B;
END LOOP;

A: Represents the sql statement in the loop body, which can be one sentence or multiple sentences. This is the core part of the loop body. These statements are executed at least once.
B: Loop end condition expression. When it is true, exit the loop, otherwise execute the loop body again.

Code example:

-- Created on 2020/12/16 by GUO 
declare 
  i int:= 0;
begin
  loop
    i:=i+1;
    dbms_output.put_line(i);
    EXIT WHEN i > 3;
  END LOOP;
end;

operation result:
Insert picture description here

Practical example:

When using Loop+ cursor, take the value in the cursor and must re-assign it again, otherwise an error will be reported.

-- Created on 2020/12/17 by GUO 
declare
  cursor user is
    select * from user_table;
  user1 user_table%rowtype;
begin
  open user;
  loop
    fetch user into user1;
    exit when user%notfound;
    dbms_output.put_line('用户名称:' || user1.USER_NAME);
    dbms_output.put_line('用户年龄:' || user1.USER_AGE);
  end loop;
  close user; --关闭游标
end;

WHILE statement

Before execution, we must first determine whether the value of the conditional expression is true, if true, execute the loop body, otherwise exit the WHILE loop and continue to execute the code behind the loop.

Basic syntax:

while a loop
  b;
end loop;

A: Indicates a conditional expression. When the value is true, the program executes the loop body, otherwise it exits.
B: The sql statement in the loop body.

Code example:

-- Created on 2020/12/17 by GUO 
declare
  i int := 0;
begin
  while i < 3 loop
    i := i + 1;
    dbms_output.put_line(i);
  end loop;
end;

operation result:
Insert picture description here

Practical example:

-- Created on 2020/12/17 by GUO 
declare
  cursor user is
    select * from user_table;
  user1 user_table%rowtype;
begin
  open user;
  fetch user into user1;
  while(user%found)loop
  dbms_output.put_line('用户名称:' || user1.USER_NAME);
  fetch user into user1;
  end loop;
end;

FOR statement

The FOR statement is a loop control statement that can set the number of loops in advance. It has a loop counter, usually an integer variable, through which the number of loops is controlled.

Basic syntax:

for A IN (reverse) B...C LOOP
   D;
 END LOOP;

A: Represents a variable, usually a certificate type, used as a counter. The default value is incremented. When the reverse keyword is used in the loop, it will be decremented in a loop.
B: Counter offline value. When the counter value is less than the lower limit, the loop is terminated.
C: The online value of the counter. When the value of the counter is greater than the upper limit, the loop is terminated.
D: Loop body.

Code example:

-- Created on 2020/12/17 by GUO 
declare 
  i int := 0;
begin
  for i IN reverse 1..5 LOOP
    dbms_output.put_line(i);
  END LOOP;
end;

operation result:
Insert picture description here

Practical example:

Use with cursor

-- Created on 2020/12/17 by GUO 
declare
  cursor user is
    select * from user_table;
begin
  for user1 in user loop
  dbms_output.put_line('用户名称:'||user1.USER_NAME);
  dbms_output.put_line('用户年龄:'||user1.USER_AGE);
  end loop;
end;

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/111305170