Business Analysis Chap04 Notes
Business Analysis Chap04 Notes
Cursors and
Exception handling
Objectives
• At the end of this lesson, you must be able to:
• Manipulate data with cursors
• Use bulk processing features
• Manage errors with exception handlers
• Construct user-defined exception handlers
• Customize PL/SQL exception messages
• Trap unanticipated errors
Manipulate data with cursors
• Oracle creates a memory area, known as the context area, for
processing an SQL statement, which contains all the information
needed for processing the statement; for example, the number of
rows processed, etc.
• A cursor is a pointer to this context area. PL/SQL controls the context
area through a cursor. A cursor holds the rows (one or more) returned
by a SQL statement. The set of rows the cursor holds is referred to as
the active set.
Manipulate data with cursors (Cont.)
• You can name a cursor so that it could be referred to in a program to
fetch and process the rows returned by the SQL statement, one at a
time. There are two types of cursors −
• Implicit cursors
• Explicit cursors
• Implicit Cursors
• Implicit cursors are automatically created by Oracle whenever an SQL
statement is executed, when there is no explicit cursor for the
statement. Programmers cannot control the implicit cursors and the
information in it.
Manipulate data with cursors (Cont.)
• Whenever a DML statement (INSERT, UPDATE and DELETE) is issued,
an implicit cursor is associated with this statement. For INSERT
operations, the cursor holds the data that needs to be inserted. For
UPDATE and DELETE operations, the cursor identifies the rows that
would be affected.
• In PL/SQL, you can refer to the most recent implicit cursor as the SQL
cursor, which always has attributes such as %FOUND, %ISOPEN,
%NOTFOUND, and %ROWCOUNT. The SQL cursor has additional
attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed
for use with the FORALL statement. The following table provides the
description of the most used attributes −
Manipulate data with cursors (Cont.)
Manipulate data with cursors (Cont.)
• Any SQL cursor attribute will be accessed as sql%attribute_name as
shown below in the example.
Manipulate data with cursors (Cont.)
• The following program will update the table and increase the salary of
each customer by 500 and use the SQL%ROWCOUNT attribute to
determine the number of rows affected −
Manipulate data with cursors (Cont.)
• When the above code is executed at the SQL prompt, it produces the
following result −
Manipulate data with cursors (Cont.)
• If you check the records in customers table, you will find that the rows
have been updated −
Manipulate data with cursors (Cont.)
• Explicit Cursors
• Explicit cursors are programmer-defined cursors for gaining more
control over the context area. An explicit cursor should be defined in
the declaration section of the PL/SQL Block. It is created on a SELECT
Statement which returns more than one row.
• The syntax for creating an explicit cursor is −
Manipulate data with cursors (Cont.)
• Working with an explicit cursor includes the following steps −
Manipulate data with cursors (Cont.)
Manipulate data with cursors (Cont.)
• Declaring the Cursor
• Declaring the cursor defines the cursor with a name and the
associated SELECT statement. For example −
Manipulate data with cursors (Cont.)
• Opening the Cursor
• Opening the cursor allocates the memory for the cursor and makes it
ready for fetching the rows returned by the SQL statement into it. For
example, we will open the above defined cursor as follows −
Manipulate data with cursors (Cont.)
• Fetching the Cursor
• Fetching the cursor involves accessing one row at a time. For example,
we will fetch rows from the above-opened cursor as follows −
Manipulate data with cursors (Cont.)
• Closing the Cursor
• Closing the cursor means releasing the allocated memory. For
example, we will close the above-opened cursor as follows −
Manipulate data with cursors (Cont.)
• Example
Manipulate data with cursors (Cont.)
• Variable Declarations:
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
• These lines declare three variables: c_id, c_name, and c_addr. The
data types of these variables are inferred from the corresponding
columns (id, name, and address) in the customers table using the
%type attribute.
Manipulate data with cursors (Cont.)
• CURSOR Declaration:
CURSOR c_customers IS
SELECT id, name, address FROM customers;
OPEN c_customers;
• This line opens the cursor c_customers, making it ready to fetch rows
from the result set.
• LOOP
• This begins a loop, which will continue to fetch and process rows from
the cursor until a specific condition is met.
Manipulate data with cursors (Cont.)
• FETCH Cursor Into Variables:
• However, this size limit can be easily achieved by introducing the ROWNUM condition
in the ‘SELECT’ statement, whereas in the case of cursor this is not possible.
• To overcome this Oracle has provided ‘LIMIT’ clause that defines the number of
records that needs to be included in the bulk.
Use bulk processing features (Cont.)
• Syntax:
• In the above syntax, the cursor fetch statement uses BULK COLLECT
statement along with the LIMIT clause.
Use bulk processing features (Cont.)
• BULK COLLECT Attributes
• Similar to cursor attributes BULK COLLECT has %BULK_ROWCOUNT(n)
that returns the number of rows affected in the nth DML statement of
the FORALL statement, i.e. it will give the count of records affected in
the FORALL statement for every single value from the collection
variable. The term ‘n’ indicates the sequence of value in the
collection, for which the row count is needed.
Use bulk processing features (Cont.)
• Example 1: In this example, we will project all the employee name
from emp table using BULK COLLECT and we are also going to
increase the salary of all the employees by 5000 using FORALL.
Use bulk processing features (Cont.)
Use bulk processing features (Cont.)
• Output
Use bulk processing features (Cont.)
• Code Explanation:
• Code line 2: Declaring the cursor guru99_det for statement ‘SELECT emp_name FROM
emp’.
• Code line 3: Declaring lv_emp_name_tbl as table type of VARCHAR2(50)
• Code line 4: Declaring lv_emp_name as lv_emp_name_tbl type.
• Code line 6: Opening the cursor.
• Code line 7: Fetching the cursor using BULK COLLECT with the LIMIT size as 5000 intl
lv_emp_name variable.
• Code line 8-11: Setting up FOR loop to print all the record in the collection lv_emp_name.
• Code line 12: Using FORALL updating the salary of all the employee by 5000.
• Code line 14: Committing the transaction.
Use bulk processing features (Cont.)
• **SAVE EXCEPTIONS with FORALL**:
• If you expect that some of the DML operations might fail (e.g.,
because of a constraint violation), you can use the `SAVE EXCEPTIONS`
clause. It will continue processing after an exception and will collect
the exceptions to be handled after the `FORALL` completes.
• These features can significantly speed up operations that affect many
rows, making them essential tools for optimizing PL/SQL code.
Exception handlers
• An exception is an error condition during a program execution.
PL/SQL supports programmers to catch such conditions using
EXCEPTION block in the program and an appropriate action is taken
against the error condition. There are two types of exceptions −
Exception handlers(Cont.)
• Syntax for Exception Handling
• The general syntax for exception handling is as follows. Here you can
list down as many exceptions as you can handle. The default
exception will be handled using WHEN others THEN −
Exception handlers(Cont.)
• Example: using the CUSTOMERS table
Exception handlers (Cont.)
• When the above code is executed at the SQL prompt, it produces the
following result −