Program with PL/SQL Lesson 4. Writing Control Structure.
-
Upload
bethanie-jenkins -
Category
Documents
-
view
221 -
download
0
Transcript of Program with PL/SQL Lesson 4. Writing Control Structure.
![Page 1: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/1.jpg)
Program with PL/SQL
Lesson 4
![Page 2: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/2.jpg)
Writing Control Structure
![Page 3: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/3.jpg)
![Page 4: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/4.jpg)
Controlling PL/SQL Flow of Execution
Change the logical execution of statements using conditional IF statements and loop control structures.
Conditional IF statements: IF – THEN – END IF IF – THEN – ELSE – END IF IF – THEN – ELSIF – END IF
![Page 5: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/5.jpg)
![Page 6: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/6.jpg)
![Page 7: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/7.jpg)
![Page 8: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/8.jpg)
![Page 9: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/9.jpg)
![Page 10: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/10.jpg)
![Page 11: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/11.jpg)
![Page 12: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/12.jpg)
CASE Expressions
A CASE expressions selects a result and returns it
To select the result. The CASE expressions uses an expression whose values is used to select one of several alternatives
CASE selector
WHEN expressions1 THEN result1
WHEN expressions2 THEN result2
…
WHEN expressionsN THEN resultN
[ELSE resultN+1;]
END;
![Page 13: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/13.jpg)
declare vgrade char(1) := upper('&p_grade'); va varchar2(20);begin va := case vgrade when 'A' then 'Excellent' when 'B' then 'very good' when 'C' then 'Good' else 'no such grade' end; dbms_output.put_line('Grade: '||vgrade||'App'||va);end;
![Page 14: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/14.jpg)
Handling Nulls
Simple comparison involving nulls always yield NULL
Applying the logical operator NOT to a null yields NULL
In conditional control statements, if the condition yields NULL, its associated sequence of statements is not executed
![Page 15: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/15.jpg)
Logic Tables
AND TRUE FALSE NULL
TRUE TRUE FALSE NULL
FALSE FALSE FALSE FALSE
NULL NULL FALSE NULL
OR TRUE FALSE NULL
TRUE TRUE TRUE TRUE
FALSE TRUE FALSE NULL
NULL TRUE NULL NULL
NOT
TRUE FALSE
FALSE TRUE
NULL NULL
![Page 16: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/16.jpg)
![Page 17: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/17.jpg)
Iterative Control: LOOP Statements
Loops repeat a statement or sequence of statements multiple times
There are three loop types Basic loop FOR loop WHILE loop
![Page 18: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/18.jpg)
Basic Loops
Syntax:
LOOP -- delimiter
Statement1; -- statements
...EXIT [WHEN condition]; -- EXIT statements
END LOOP -- delimiter
![Page 19: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/19.jpg)
Contoh….
DECLARE ord_id order.order_id%type := 97; vcount number(2) := 0;BEGIN LOOP INSERT INTO order(order_id,item_id,product_id,actual_price) VALUES (ord_id,vcount,1011,0); vcount := vcount + 1; EXIT WHEN vcount > 10; END LOOP;END;/
![Page 20: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/20.jpg)
WHILE Loops
Syntax:
WHILE condition LOOP
Statement1;
Statement2;
...END LOOP
Condition is evaluated at the beginning of each iteration
Use the WHILE loop to repeat statements while a condition is TRUE
![Page 21: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/21.jpg)
Contoh…
DECLARE ord_id order.order_id%type := 97; vcount number(2) := 1;BEGIN WHILE vcount <= 10 LOOP INSERT INTO order(order_id,item_id,product_id,actual_price) VALUES (ord_id,vcount,1011,0); vcount := vcount + 1; END LOOP;END;/
![Page 22: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/22.jpg)
FOR Loops
Syntax:FOR counter IN [REVERSE]
lower_bound .. Upper_bound LOOPStatement1;
Statement2;
...
END LOOP
Use a FOR loop to shortcut the test for the number of iterations
Do not declare the counter; it is declared implicitly ‘lower_bound .. Upper_bound’ is required syntax
![Page 23: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/23.jpg)
Contoh…
DECLARE
ord_id order.order_id%type := 97;
BEGIN
FOR vcount IN 1..10 LOOP INSERT INTO order(order_id,item_id,product_id,actual_price)
VALUES (ord_id,vcount,1011,0);
END LOOP;
END;
/
![Page 24: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/24.jpg)
FOR Loops
Guidelines Reference the counter within the loop only; it
is undefined outside the loop Do not reference the counter as the targert of
an assignment
![Page 25: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/25.jpg)
Guidelines While Using Loops
Use the basic loop when the statements inside the loop must execute at least once
Use the WHILE loop if the condition has to be evaluated at the start of each iteration
Use a FOR loop if the number of iterations is known
![Page 26: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/26.jpg)
Nested Loops and Labels
Nest loops to multiple levels
Use labels to distinguish between blocks and loops
Exit the outer loop with the EXIT statement that references the label
![Page 27: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/27.jpg)
declare the_counter number(2) := 0; total_done varchar2(3) := 'NO'; inner_done varchar2(3) := 'NO';begin <<outer_loop>> loop exit when the_counter > 10; dbms_output.put_line('outer loop : the_counter = '||the_counter); the_counter := the_counter + 1; <<inner_loop>> loop exit inner_loop when inner_done = 'YES'; dbms_output.put_line('inner loop:the_counter= '|| the_counter); if the_counter = 5 then inner_done := 'YES'; dbms_output.put_line('will exit inner loop'); else the_counter := the_counter + 1; end if; end loop inner_loop; exit outer_loop when total_done = 'YES'; end loop outer_loop;end;
![Page 28: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/28.jpg)
Practice Overview
![Page 29: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/29.jpg)
Practice 1
Create a PL/SQL block that rewards an employee by appending an asterisk in the STARS column for every $1000 of the employee’s salary.
a. Use the DEFINE command to provide the employee_id. Pass the value to the PL/SQL block through a iSQL*Plus substitution variable
b. Initialize a v_asterisk variable that contains a NULLc. Append an asterisk to the string for every $1000 of the
salary amount. For example, if the employee has a salary amount $8000, the string of asterisk should contain eight asterisk. If the employee has a salary amount of $12500, the string of asterisks should contains 13 asterisks
d. Update the STARS column for the employee with the string of asterisk
e. COMMIT
![Page 30: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/30.jpg)
Practice 2
In a loop, use a cursor to retrieve the department number and department name from the DEPARTMENTS table for those departments whose DEPARTMENT_ID is less than 100. Pass the department number to another cursor to retrieve from the EMPLOYEES table the details of employee last name, job, hire_date, and salary of those employees whose EMPLOYEE_ID is less than 120 and who work in that department.
![Page 31: Program with PL/SQL Lesson 4. Writing Control Structure.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649f045503460f94c17bb4/html5/thumbnails/31.jpg)
Practice 3
In a loop use the iSQL*Plus substitution parameter created in step 1 and gather the salaries of the top n people from the EMPLOYEES table. There should be no duplication in the salaries. If two employees earn the same salary, the salary should be picked up only once and store the salaries in the TOP_DOGS table