Home >Database >Oracle >How to write loop statement in oracle

How to write loop statement in oracle

下次还敢
下次还敢Original
2024-04-30 08:39:15712browse

There are three types of loop statements in Oracle. The syntax is: FOR loop: FOR loop_variable IN [start_value, end_value] LOOP statement(s);END LOOP;WHILE loop: WHILE condition LOOP statement(s);END LOOP ;DO WHILE loop: DO statement(s);WHILE condition;END;

How to write loop statement in oracle

##Loop statements in Oracle

Loop statements in Oracle are used to repeatedly execute a set of statements until a specific condition is met. It can be used to process large amounts of data or perform the same tasks repeatedly.

Syntax

There are three main loop statements in Oracle:

  • FOR loop: Traverse a sequence in order Series value.
  • WHILE loop: Continue execution until the conditions are met.
  • DO WHILE loop: First execute a set of statements, and then check the condition.

FOR Loop

<code class="sql">FOR loop_variable IN [start_value, end_value] LOOP
  statement(s);
END LOOP;</code>
For example:

<code class="sql">FOR i IN 1..10 LOOP
  dbms_output.put_line('i = ' || i);
END LOOP;</code>
This will print "i = 1" to "i = 10" in the console .

WHILE loop

<code class="sql">WHILE condition LOOP
  statement(s);
END LOOP;</code>
For example:

<code class="sql">DECLARE
  i NUMBER := 1;
BEGIN
  WHILE i <= 10 LOOP
    dbms_output.put_line('i = ' || i);
    i := i + 1;
  END LOOP;
END;</code>

DO WHILE loop

<code class="sql">DO
  statement(s);
WHILE condition;
END;</code>
For example:

<code class="sql">DECLARE
  i NUMBER := 1;
BEGIN
  DO
    dbms_output.put_line('i = ' || i);
    i := i + 1;
  WHILE i <= 10;
END;</code>
It should be noted that the WHILE and DO WHILE loops must include statements that modify the loop variables to ultimately meet the conditions. Otherwise, the loop will execute indefinitely.

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

Statement:
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