Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing
Chapter 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
Brewbean’s Challenge
Control Structures IF Statements CASE Statements Basic IF IF/THEN/ELSE IF/THEN/ELSIF/ELSE CASE Statements Basic CASE statement Searched CASE statement CASE expression
Simple IF Statement
Add the ELSE clause
Add ELSIF clauses
NOT ELSEIF
Can 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;
Basic CASE Statement
No CASE Found Error
Searched CASE Statement
CASE Expression
Basic Loop
WHILE Loop
FOR Loop
Use 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;
Reverse 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
GOTO 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
Exception 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
Predefined Oracle Errors Exception Name Description NO_DATA_FOUND A SELECT statement in a PL/SQL block retrieves no rows or a nonexistent row of an index-by table is referenced. TOO_MANY_ROWS A SELECT statement in a PL/SQL block retrieves more than one row. CASE_NOT_FOUND No WHEN clause in the CASE statement is processed. ZERO_DIVIDE Attempted division by zero. DUP_VAL_ON_INDEX Attempted violation of a unique or primary key column constraint.
Predefined Error Example
Add Exception Handler
Non-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
User-Defined Exception Declare an exception If no rows updated, raise the exception Exception handler
Additional 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
SQLCODE and SQLERRM Log error information to a database table using these functions
RAISE_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
Exception 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
Commenting 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
Comment 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 SQL%NOTFOUND THEN RAISE ex_prod_update; END IF; EXCEPTION WHEN ex_prod_update THEN DBMS_OUTPUT.PUT_LINE('Invalid product id entered'); END;
Summary 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