0% found this document useful (0 votes)
26 views

Writeups - Assignment 7

The document discusses different types of database cursors including implicit, explicit, cursors for loops, and parameterized cursors. It provides definitions and examples of each type. Implicit cursors are automatically created by Oracle while explicit cursors must be defined and controlled by the programmer. Cursors allow retrieving multiple rows from a query one at a time rather than all at once.

Uploaded by

ganesh bagul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Writeups - Assignment 7

The document discusses different types of database cursors including implicit, explicit, cursors for loops, and parameterized cursors. It provides definitions and examples of each type. Implicit cursors are automatically created by Oracle while explicit cursors must be defined and controlled by the programmer. Cursors allow retrieving multiple rows from a query one at a time rather than all at once.

Uploaded by

ganesh bagul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

There are two types of cursor namely as,

1) Implicit cursor.

2) Explicit cursor.

Need the Cursors

 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,

DECLARE cursor_name CURSOR FOR select_statement;

Working with an explicit cursor includes the following 4 steps

 Declaring the cursor for initializing the memory


 Opening the cursor for allocating the memory
 Fetching the cursor for retrieving the data
 Closing the cursor to release the allocated memory

Declaring the Cursor

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

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;

Fetching the Cursor

Fetching the cursor involves accessing one row at a time.

FETCH c_customers INTO c_id, c_name, c_addr;

Closing the Cursor

Closing the cursor means releasing the allocated memory.

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.

Cursors with Parameters Example

DECLARE

cur_emp (par_dept VARCHAR2) CURSOR FOR

SELECT ename, salary

FROM emp

WHERE deptno = par_dept ;

Open cur_emp(5);

Declaring variables in a Cursor:

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

project_bonus NUMBER := 1000;

Conclusion: Thus we have studied and implement all types: Implicit, Explicit, Cursor FOR
Loop, and Parameterized Cursor.

You might also like