Home > Database > Oracle > How to use while loop in oracle stored procedure

How to use while loop in oracle stored procedure

WBOY
Release: 2022-05-18 18:02:38
Original
8437 people have browsed it

In Oracle stored procedures, the while loop is used to enter the loop when the conditions are met. If the conditions are not met, the loop is jumped out. The syntax is "WHILE conditional LOOP loop body END LOOP;"; the general expression of the while statement is: "while(expression){loop body}".

How to use while loop in oracle stored procedure

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.

How to use while loop in oracle stored procedure

While is a basic loop mode. When the condition is met, it enters the loop. After entering the loop, when the condition is not met, it exits the loop.

The general expression of while statement is: while (expression) {loop body}.

WHILE syntax in oracle:

WHILE  条件  LOOP
    ...
END  LOOP ;
Copy after login

For example:

set serverout on
declare
  --定义初始值
  v_num number:=#
begin
  --编写循环结构和定义循环条件
  while v_num<10 loop
     dbms_output.put_line(&#39;第&#39;||v_num||&#39;次输出&#39;);
     --改变循环条件
    v_num:=v_num+1; 
  end loop;
end;
Copy after login

The example is as follows:

set serveroutput on
declare
  num int;
  total int;
begin
  num:=0;
  total:=0;
  while num<5 loop 
    num:=num+1;
    total:=total+num;
  end loop;
  dbms_output.put_line(&#39;前5个自然数的和是&#39;||total);
end;
Copy after login

How to use while loop in oracle stored procedure

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of How to use while loop in oracle stored procedure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template