Writeups - Assignment 7
Writeups - Assignment 7
Title: Design the Database Cursor. Implement all types: Implicit, Explicit, Cursor FOR Loop,
and Parameterized Cursor.
Theory:
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.
Types of Cursors:
1) Implicit cursor.
2) Explicit cursor.
SELECT statement should return only one row at a time in previous PL/SQL programs.
This is too restrictive in many applications.
We use the idea of Cursor to handle the above problem
Implicit Cursor:
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.
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.
Sr No. Attributes and Description
1 %FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement
affected one or more rows or a SELECT INTO statement returned one
or more rows. Otherwise, it returns FALSE.
2 %NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT,
UPDATE, or DELETE statement affected no rows, or a SELECT
INTO statement returned no rows. Otherwise, it returns FALSE.
3 %ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the
SQL cursor automatically after executing its associated SQL
statement.
4 %ROWCOUNT Returns the number of rows affected by an INSERT,
UPDATE, or DELETE statement, or returned by a SELECT INTO
statement.
Any SQL cursor attribute will be accessed as sql%attribute_name as shown below in the
example.
Syntax:
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,
Declaring the cursor defines the cursor with a name and the associated SELECT statement. For
example −
DECLARE c_customers CURSOR FOR SELECT id, name, address FROM customers;
Opening the cursor allocates the memory for the cursor and makes it ready for fetching the rows
returned by the SQL statement into it.
OPEN c_customers;
CLOSE c_customers;
Cursors with Parameters:
1) We can pass parameters into a cursor and use them in the query.
2) We can only pass values to the cursor; and cannot pass values out of the cursor through
parameters.
3) Only the data type of the parameter is defined, not its length.
4) Optionally, we can also give a default value for the parameter, which will take effect if no
value is passed to the cursor.
DECLARE
FROM emp
Open cur_emp(5);
In native SQL, the SELECT list may contain both columns and expressions. In PL/SQL, the
SELECT list may contain PL/SQL variables, expressions, and even functions as well as host
language bind variables (> PL/SQL 2.1)
DECLARE
Conclusion: Thus we have studied and implement all types: Implicit, Explicit, Cursor FOR
Loop, and Parameterized Cursor.