Pl-sql Programming Basics (3)

Source: Internet
Author: User

Loop structure:

1, Loop loop structure

Grammar:

LOOP

The statement to execute;

Exit when < conditions >--Exits the loop if the condition is met

END LOOP;

Example: An integer that loops out 1-10

DECLARE  Number: = 1;   BEGIN      LOOP         dbms_output.put_line (v_num);         V_num:=+1;          EXIT  when Ten ;      END LOOP;      END;

The drawback of loop structure: The structure is not clear enough, exit easy to write.

2. While loop structure

Grammar:

While < Boolean expressions > LOOP

The statement to execute;

END LOOP;

Example: Displaying an integer of 1-10;

DECLARE  Number: = 1;   BEGIN      while <= Ten LOOP         Dbms_output.put_line (v_num);         V_num:=+1;      END LOOP;      END;

3. For loop

Grammar:

For loop counter in [REVERSE] offline ... Upper LOOP

The statement to execute;

END LOOP;

Example: Displaying an integer of 1-10

DECLARE  number; BEGIN   for inch 1.. Ten LOOP          Dbms_output. Put_Line (v_num);     END LOOP; END;

In the For loop, once per loop, the loop variable is automatically +1, using the keyword reverse, the loop variable automatically-1, the number following in reverse must be small to large, and must be an integer, cannot be a variable or an expression, you can use Exit to exit the loop.

In the previous example, if you add reverse, the output is descending.

Summarize:

The loop structure can perform certain operations repeatedly, taking precedence over a for loop in the known number of cycles, and using a while loop for unknown times. Exit can be nested in any loop, terminating the loop directly. Loop is not used in general!

Pl-sql Programming Basics (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.