Title: Oracle9i Developer:
1Oracle9i Developer PL/SQL Programming
Chapter 3
PL/SQL Processing
2Chapter Objectives
- Use control structures to make decisions
- Use looping structures to repeat code
- Work with the GOTO statement
- Manage errors with exception handlers
- Address exception-handling issues, such as
RAISE_APPLICATION_ERROR and propagation - Document code with comments
3Brewbeans Challenge
4Control Structures
- IF Statements
- Basic IF
- IF/THEN/ELSE
- IF/THEN/ELSIF/ELSE
- CASE Statements
- Basic CASE statement
- Searched CASE statement
- CASE expression
5Simple IF Statement
6Add the ELSE clause
7Add ELSIF clauses
8NOT ELSEIF
9Can check multiple conditions
- IF rec_order.state IN ('VA','PA','ME') THEN
- lv_tax_num rec_order.sub .06
- ELSE
- lv_tax_num rec_order.sub .04
- END IF
10Basic CASE Statement
11No CASE Found Error
12Searched CASE Statement
13CASE Expression
14Basic Loop
15WHILE Loop
16FOR Loop
17Use Variables in FOR Loop
DECLARE lv_upper_num NUMBER(3) BEGIN SELECT
COUNT(idBasket) INTO lv_upper_num FROM
bb_basket FOR i IN 1..lv_upper_num LOOP
DBMS_OUTPUT.PUT_LINE(i) END LOOP END
18Reverse Option in FOR Loop
- Instructs counter to start with the upper bound
value and increment by 1 to the lower bound
value - Example
- FOR I IN REVERSE 1..5 LOOP
19GOTO Statement
- Jumping control that instructs the program to
move to another area of code to continue
processing - Most developers discourage the use of GOTO as it
complicates the flow of execution
20Exception Handlers
- Used to capture error conditions and handle the
processing to allow the application to continue - Placed in the EXCEPTION section of a PL/SQL block
- Two types of errors
- Oracle errors (Predefined and Non-Predefined)
- User-defined errors
- RAISE_APPLICATION_ERROR
21Predefined Oracle Errors
22Predefined Error Example
23Add Exception Handler
24Non-Predefined Handler
Declare an exception
Associate an Oracle error number to the
exception name
Foreign key error occurs if item lines are still
in the bb_basketitem table
Exception handler executes if Foreign key error
2292 is raised by the DELETE
25User-Defined Exception
Declare an exception
If no rows updated, raise the exception
Exception handler
26Additional Exception Concepts
- WHEN OTHERS traps all errors not specifically
addressed by an exception handler and used for
handling unanticipated errors - SQLCODE and SQLERRM
- Functions used to identify the error code and
message especially in application testing to
identify unanticipated errors
27SQLCODE and SQLERRM
Log error information to a database table using
these functions
28RAISE_APPLICATION_ERROR
- Allows developers to associate their own error
number and message to an error - Can only be used with stored programs which is
covered in Chapter 4
29Exception Propagation
- When an error occurs in a block, processing
immediately moves to the EXCEPTION section of the
block - If the block is nested, and no handlers address
the error, the processing will then move to the
EXCEPTION section of the enclosing block
30Commenting Code
- Add comments within code to identify code purpose
and processing steps - Use / / to enclose a multiline comment
- Use -- to add a single or partial line comment
31Comment Examples
- DECLARE
- ex_prod_update EXCEPTION --For UPDATE of no
rows exception - BEGIN
- / This block is used to update product
descriptions - Constructed to support the Prod_desc.frm app
screen - Exception raised if no rows updated /
- UPDATE bb_product
- SET description 'Mill grinder with 5 grind
settings!' - WHERE idProduct 30
- --Check if any rows updated
- IF SQLNOTFOUND THEN
- RAISE ex_prod_update
- END IF
- EXCEPTION
- WHEN ex_prod_update THEN
- DBMS_OUTPUT.PUT_LINE('Invalid product id
entered') - END
32Summary
- Add conditional logic using IF and CASE
statements - Repeat lines of code using Loop structures
- Add error handlers in the EXCEPTION area to
manage Oracle and user-defined errors - Use comments in code for documentation