Oracle PL/SQL syntax

Source: Internet
Author: User
Control Structure Control code lines of PL/SQL program flows. PL/SQL supports Conditional Control and cyclic control structure. Syntax and purpose if .. then Syntax: If condition then statements 1; Statements 2 ;.... the end if statement determines whether the condition is true. If yes, the statement after then is executed. If the condition is false or null, the statement between then and end if is skipped, execute the statement after end if. If .. then... else Syntax: If condition then statements 1; Statements 2 ;.... else statements 1; Statements 2 ;.... if the condition is true, the end if statement is executed between then and else. Otherwise, the statement between else and end if is executed. If can be nested, you can use if or if. Else statements in if or if. Else statements. If (A> B) and (A> C) Then G: = A; else G: = B; if C> G then G: = C; end ifend if .. then .. elsif Syntax: If condition1 then statement1; elsif condition2 then statement2; elsif condition3 then statement3; else; end if; else; if condition condition1 is true, statement1 is executed, then begin, otherwise, judge whether condition2 is true. If it is true, execute statement2 and then execute statement5. The same is true for condition3. If condition1, condition2, and condition3 are not true, execute statemen. T4, and then execute statement5. Loop Control the basic form of loop control is loop statements. The statements between loop and end loop are executed infinitely. The syntax of the loop statement is as follows: loop statements; the statement between the end loop and the end loop cannot be executed infinitely. Therefore, you must use the exit statement when using the loop statement, force loop end, for example, X: = 100; loop X: = x + 10; if x> 1000 then exit; end ifend loop; Y: = X; the value of Y is 1010. the exit when statement ends the loop. If the condition is true, the loop ends. X: = 100; loopx: = x + 10; exit when x> 1000; X: = x + 10; end loop; Y: = x; while .. loop while .. loop has a condition associated with the loop. If the condition is true, the statement in the loop body is executed. If the result is false, the loop ends. X: = 100; while x <= 1000 loop X: = x + 10; end loop; y = x;... loop Syntax: For counter in [reverse] start_range .... end_range loopstatements; end loop; the number of loops in the loop and while loop are uncertain, the number of loops in the for loop is fixed, and the counter is an implicitly declared variable whose initial value is start_range, the second value is start_range + 1 until end_range. If start_range is equal to end _ range, the loop is executed once. If the reverse keyword is used, the range is a descending order. X: = 100; for v_counter in 1 .. 10 loopx: = x + 10; end loopy: = x; to exit the for loop, use the exit statement. Tag users can use tags to make programs more readable. The block or loop can be marked. The label format is <>. Mark the program block <> [declare] ...... begin ........ [Exception]... end label_name mark loop <> loop ......... <> loop .......... <> loop .... exit outer_loop when v_condition = 0; end loop innermost_loop ;.......... end loop inner_loop; end loop outer_loop; GOTO statement Syntax: goto label; When a GOTO statement is executed, the control is immediately transferred to the statement marked by the label. PL/SQL has some restrictions on the GOTO statement. For block, loop, and if statements, it is illegal to jump from the outer layer to the inner layer. X: = 100; for v_counter in 1 .. 10 loop if v_counter = 4 Then goto end_of_loop end if X: = x + 10; <> nullend loopy: = x; Note: NULL is a legal executable statement. Nested blocks can have another internal block. This is called nesting. Note that variables are nested. variables defined in the most external block can be used in all child blocks. If the child block defines the same variable name as the external block variable, when executing a sub-block, the variables defined in the sub-block are used. The variables defined in the Child block cannot be referenced by the parent block. Similarly, the GOTO statement cannot jump from the parent block to the sub-block. Otherwise, it is legal. Outer block declare a_number integer; B _number integer; begin -- a_number and B _number are available here <> declare c_number integer B _number Number (20) Begin c_number: = a_number; c_number = outer_block. B _number; end sub_block; end out_block;

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.