1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Controlling flow in PL / SQL pptx

34 353 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 34
Dung lượng 197,54 KB

Nội dung

Controlling Flow in PL/SQL Blocks 23 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć2 Controlling Flow in PL/SQL Blocks 23Ć3 Objectives You can control the flow of your PL/SQL block by using conditional statements and loops. At the end of this lesson, you should be able to D Conditionally control processing in a PL/SQL block. D Iterate statements by using various types of loops. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć4 Controlling Flow in PL/SQL Blocks 23Ć5 Overview You can change the logical flow of statements within the PL/SQL block with a number of control structures. This lesson addresses two types of PL/SQL control structures: D Conditional constructs with the IF statement D Looping constructs D Basic loop to provide repetitive actions without overall conditions D FOR loops to provide for iterative control of actions based upon a count D WHILE loops to provide iterative control of actions based on a true statement D EXIT statement to terminate loops For more information, see PL/SQL User’s Guide and Reference, Release 2.3, Chapter 3 “Control Structures.” Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć6 Controlling Flow in PL/SQL Blocks 23Ć7 The IF Statement The structure of the PL/SQL IF statement is similar to the structure of IF statements in other procedural languages. It allows PL/SQL to perform actions selectively based upon conditions. Syntax IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statements;] END IF; where: condition is a Boolean variable or expression (TRUE, FALSE, or NULL). Guidelines D When writing code, remember the spelling of the keywords. D ELSIF is one word. D END IF is two words. D If the controlling Boolean condition is TRUE, the associated sequence of statements is executed; if the controlling Boolean condition is FALSE or NULL, the associated sequence of statements is passed over. D Any number of ELSIF clauses are permitted. D There can be at most one ELSE clause. D Indent the conditionally executed statements for clarity. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć8 Controlling Flow in PL/SQL Blocks 23Ć9 The IF Statement continued Simple IF Statements PL/SQL executes the conditional statements only if the condition is TRUE. If the condition is FALSE or NULL, then PL/SQL ignores the conditional statements. In either case, control resumes at the next statement in the program following END IF. Example Set the job title to Sales Representative and the region number to 35 if the last name is Dumas. . . . IF v_last_name = ’Dumas’ THEN v_job := ’Sales Representative’; v_region_id := 35; END IF; . . . IFĆTHENĆELSE Statements If the condition is FALSE or NULL, you can use the ELSE clause to carry out other actions. As with the simple IF statement, control resumes in the program from the END IF. Example Set a flag for orders where there are fewer than five days between order date and ship date. . . . IF v_date_shipped - v_date_ordered < 5 THEN v_ship_flag := ’Acceptable’; ELSE v_ship_flag := ’Unacceptable’; END IF; . . . Introduction to Oracle: SQL and PL/SQL Using Procedure Builder23Ć10 [...]... 23Ć13 23Ć14 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Building Logical Conditions continued The AND logic table can help you evaluate the possibilities for the Boolean condition you see below v_flag := v_reorder_flag AND v_available_flag; Controlling Flow in PL/ SQL Blocks 23Ć15 23Ć16 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Loop Statements PL/ SQL provides... variables involved in the conditions do not change during the body of the loop, then the condition will remain TRUE, and the loop will not terminate Controlling Flow in PL/ SQL Blocks 23Ć25 23Ć26 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Loop Statements continued Nested Loops and Labels You can nest loops to multiple levels You may nest FOR loops within WHILE loops, and WHILE loops within... bound for the range of index values Note: Do not declare the index; it is declared implicitly as an integer Controlling Flow in PL/ SQL Blocks 23Ć21 23Ć22 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Loop Statements continued Example Print the number of times the loop is executed and the last value for the index based on the supplied lower bound and upper bound PROCEDURE iterate (v_lower... 10; END LOOP; Controlling Flow in PL/ SQL Blocks 23Ć19 23Ć20 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Loop Statements continued FOR Loop FOR loops have the same general structure as the loops you have already seen In addition, they have a control statement at the front of the LOOP keyword to determine the number of iterations PL/ SQL performs Syntax FOR index IN [REVERSE] lower_bound... TRUE Controlling Flow in PL/ SQL Blocks 23Ć29 23Ć30 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Practice Overview In this practice, you create procedures that incorporate loops and conditional control structures Practice Contents D Performing conditional actions using the IF statement D Performing iterative steps by using the loop structure D Viewing the values variables during runtime... LOOP; where: condition Controlling Flow in PL/ SQL Blocks is a Boolean variable or expression (TRUE, FALSE, or NULL) 23Ć17 23Ć18 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Loop Statements continued Example Insert the first ten new line items for order number 101 v_ord_id s_item.ord_id%TYPE := 101; v_counter NUMBER (2) := 1; BEGIN LOOP INSERT INTO s_item (ord_id, item_id) VALUES... optionally be included after the END LOOP statement BEGIN LOOP v_counter :=v_counter+1; EXIT WHEN v_counter>10; LOOP EXIT Outer_loop WHEN total_done = ’YES’; –– Leave both loops EXIT WHEN inner_done = ’YES’; –– Leave inner loop only END LOOP Inner_Loop; END LOOP Outer_loop; END; Controlling Flow in PL/ SQL Blocks 23Ć27 23Ć28 Introduction to Oracle: SQL and PL/ SQL Using Procedure... assignment Controlling Flow in PL/ SQL Blocks 23Ć23 Condition is evaluated at the beginning of each iteration 23Ć24 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Loop Statements continued WHILE Loop You can use the WHILE loop to repeat a sequence of statements until the controlling condition is no longer TRUE The condition is evaluated at the start of each iteration The loop terminates... starting value If the entered value is less than 50, then the calculated value is 10% of the starting value FUNCTION calc_val (v_start IN NUMBER) RETURN NUMBER IS BEGIN IF v_start > 100 THEN RETURN (2 * v_start); ELSIF v_start >= 50 THEN RETURN (.5 * v_start); ELSE RETURN (.1 * v_start); END IF; END calc_val; Controlling Flow in PL/ SQL Blocks 23Ć11 23Ć12 Introduction to Oracle: SQL and PL/ SQL Using... by using Procedure Builder debugging features Controlling Flow in PL/ SQL Blocks 23Ć31 23Ć32 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Practice 23 If you are not already connected to the database, be sure to do so now 1 Create a procedure named SET_COMM to conditionally set the commission percentage for a given employee based upon total sales a Prepare this exercise by disabling . Controlling Flow in PL/ SQL Blocks 23 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder23Ć2 Controlling Flow in PL/ SQL Blocks 23Ć3 Objectives You. v_available_flag; . . . Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder23Ć16 Controlling Flow in PL/ SQL Blocks 23Ć17 Loop Statements PL/ SQL provides

Ngày đăng: 17/01/2014, 09:20

TỪ KHÓA LIÊN QUAN

w