Oracle PL/SQL Control Structure

Source: Internet
Author: User


Oracle PL/SQL control structure PL/SQL is a process-oriented language with a logic-oriented control language. The main logical control statements include IF, IF .... ELSE..., IF .... ELIF ....., nested IF, CASE, LOOP, WHILE... LOOP,... LOOP, cursor for loop. I. The IF condition control statement has three usage methods: IF, IF... ELSE... and IF... ELIF ..... These three methods can be flexibly selected based on the actual situation. Www.2cto.com (1) IF syntax structure: IF condition THEN statement; end if; (2) IF... ELSE... structure syntax structure: IF condition THEN statements; ELSE statements; end if; (3) IF .... ELIF .... structure: IF condition THEN statements; ELIF condition2 THEN statements ;...... [ELSE statements;] end if; www.2cto.com (4) IF nesting 2. CASE condition control statements can be divided into two types: 1) a simple CASE statement. It provides an expression and compares the expression result with several foreseeable results. If the comparison is successful, the corresponding statement sequence is executed. [<Label_name>] CASE case_operand WHEN when_operand THEN statement; [WHEN when_operand THEN statement;] [ELSE statement;] end case [label_name] www.2cto.com description: <label_name> is a tag that can be added selectively. If a label is added, it is recommended that the label be marked at the end of the CASE statement, indicating that a CASE statement is ended. 2) The CASE statement of the search type will check whether the Boolean value is TRUE in sequence. If the Boolean value is TRUE, the WHEN clause in which it is located will be executed, and the Boolean expression after it will not be considered. If none of the boolean expressions are TRUE, the program will go to the ELSE clause. If there is no ELSE clause, the system will give a CASE_NOT_FOUND exception. [<Label_name>] case when boolean_expression THEN statement ;..... www.2cto.com [ELSE statement;] end case [label_name]; 3. LOOP control statements include LOOP, WHILE... LOOP, ..... LOOP, cursor for loop. 1. Basic LOOP this form of LOOP is the most basic structure in the LOOP control statement. It will repeatedly execute the statement sequence between the LOOP and the end loop. The basic LOOP statements do not contain conditions for the interrupted LOOP. Therefore, they are usually used together with other conditional control statements. LOOP loops can be interrupted using EXIT and GOTO. <Label_name> LOOP www.2cto.com statement...; end loop [label_name]; Basic LOOP statements must be used with conditional control statements. Otherwise, an endless LOOP may occur. Generally, the following two methods are used to stop a loop: (1) Use the IF statement and EXIT statement in the loop to end the loop, indicating that the loop body exits when a condition is met, then execute the command backward. By default, EXIT exits the current LOOP. If tags are used, you can EXIT the specified LOOP. (2) Use the EXIT... WHEN statement to end the loop. This method is often used in cursors. It indicates that EXIT is triggered WHEN the condition after the WHEN is TRUE. If no LOOP is added after EXIT, the current LOOP is aborted. Example: EXIT basic_loop WHEN v_num> 5; 2. WHILE... loop while... the statement in the LOOP structure can terminate the LOOP. When the Boolean expression after the WHILE clause is TRUE, the statement set between the LOOP and the end loop will be executed once, then, judge whether the expression after the WHILE operation is TRUE. The syntax structure is as follows: [<label_name>] WHILE boolean_expression LOOP www.2cto.com statement; end loop [label_name]; Description: boolean_expression In the syntax is a Boolean expression, the statement part can be executed only when the expression is TRUE. 3. The FOR... LOOP statement cyclically traverses integers in the specified range. FOR... the syntax structure of the LOOP is as follows: [<label_name>] FOR index_name IN [REVERSE] lower_bound .. upper_bound LOOP www.2cto.com statement; end loop [label_name]; Note: index_name is a cyclic counter, so you cannot assign a value to it. REVERSE: This item specifies the loop mode. The default mode is from subscript to superscript. If the REVERSE keyword is used, the loop mode is opposite and the superscript is used. Use the ".." link between the subscript and the superscript.

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.