Chapter 16 Cursors and Exceptions
Chapter Objectives Determine when an explicit cursor is required Declare, open, and close an explicit cursor Fetch data from an explicit cursor Identify attributes associated with a cursor Determine when an explicit cursor is required Declare, open, and close an explicit cursor Fetch data from an explicit cursor Identify attributes associated with a cursor
Chapter Objectives Use a cursor FOR loop to retrieve data from a cursor Declare a cursor in the subquery of a cursor FOR loop Evaluate BOOLEAN conditions combined with logical operators Identify the purpose of the exception- handling section of a PL/SQL block Use a cursor FOR loop to retrieve data from a cursor Declare a cursor in the subquery of a cursor FOR loop Evaluate BOOLEAN conditions combined with logical operators Identify the purpose of the exception- handling section of a PL/SQL block
Chapter Objectives Trap predefined exceptions in a PL/SQL block Trap user-defined exceptions in a PL/SQL block Trap predefined exceptions in a PL/SQL block Trap user-defined exceptions in a PL/SQL block
Cursor An area in memory that holds the values currently being processed Must be explicitly declared when retrieving more than one row An area in memory that holds the values currently being processed Must be explicitly declared when retrieving more than one row
Opening an Explicit Cursor When opened, the memory is allocated, the SELECT statement is executed, and necessary data is loaded into the cursor
Closing the Cursor
Fetching Data from the Cursor Data is retrieved from an explicit cursor using the FETCH command Assigns the values to previously declared variables Data is retrieved from an explicit cursor using the FETCH command Assigns the values to previously declared variables
Cursor Attributes
Cursor Example Declaring a cursor Opening a cursor Fetching data Closing a cursor
Cursor FOR Loop Automatically opens and closes a cursor, and fetchs data from a cursor
Example Cursor FOR Loop
%ROWTYPE Attribute Specifies that a record will have the same structure as the row being retrieved
Logic Table Determines how a statement will be evaluated when conditions are joined with logical operators
Exception Handling Exception – signal that an error has occurred Does not include syntax errors Common examples – NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE Exception – signal that an error has occurred Does not include syntax errors Common examples – NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE
Exception-Handling Section Syntax
Exception Example
User-Defined Exception Handling Name of exception must be declared with datatype EXCEPTION Declared exception must be associated with Oracle server error number using PRAGMA EXCEPTION_INIT statement Must be included in the block’s exception-handling section Name of exception must be declared with datatype EXCEPTION Declared exception must be associated with Oracle server error number using PRAGMA EXCEPTION_INIT statement Must be included in the block’s exception-handling section
Syntax for the PRAGMA EXCEPTION_INIT Statement
Example of Trapping a User-Defined Exception