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

Unit-4 Database Cursors

BCA 8th sem Database Programming Note

Uploaded by

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

Unit-4 Database Cursors

BCA 8th sem Database Programming Note

Uploaded by

demodgdg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit – 4 Database Cursors [5 Hrs]

PL/SQL Cursor
When an SQL statement is processed, Oracle creates a memory area known as context area.
A cursor is a pointer to this context area. It contains all information needed for processing the
statement. In PL/SQL, the context area is controlled by Cursor.
A cursor contains information on a select statement and the rows of data accessed by it.

A cursor is used to referred to a program to fetch and process the rows returned by the SQL
statement, one at a time. There are two types of cursors:
o Implicit Cursors
o Explicit Cursors

PL/SQL Implicit Cursors


The implicit cursors are automatically generated by Oracle while an SQL statement is
executed, if you don't use an explicit cursor for the statement.
These are created by default to process the statements when DML statements like INSERT,
UPDATE, DELETE, SELECT etc. are executed.

Orcale provides some attributes known as Implicit cursor's attributes to check the status
of DML operations. Some of them are: %FOUND, %NOTFOUND, %ROWCOUNT and
%ISOPEN.

For example: When you execute the SQL statements like INSERT, UPDATE, DELETE then the
cursor attributes tell whether any rows are affected and how many have been affected. If you
run a SELECT INTO statement in PL/SQL block, the implicit cursor attribute can be used to find
out whether any row has been returned by the SELECT statement. It will return an error if
there no data is selected.

The following table specifies the status of the cursor with each of its attribute.

Attribute Description
%FOUND Its return value is TRUE if DML statements like INSERT, DELETE and UPDATE
affect at least one row or more rows or a SELECT INTO statement returned
one or more rows. Otherwise it returns FALSE.
%NOTFOUND Its return value is TRUE if DML statements like INSERT, DELETE and UPDATE
affect no row, or a SELECT INTO statement return no rows. Otherwise it
returns FALSE. It is a just opposite of %FOUND.
%ISOPEN It always returns FALSE for implicit cursors, because the SQL cursor is
automatically closed after executing its associated SQL statements.
%ROWCOUNT It returns the number of rows affected by DML statements like INSERT,
DELETE, and UPDATE or returned by a SELECT INTO statement.

Prepared By: Raju Poudel (Lecturer - MMC) 1


Consider the following table:

ID NAME SALARY
1 Ram 20000
2 Shyam 22000
3 Hari 24000
4 Sita 26000
5 Gita 28000

Let's execute the following program to update the table and increase salary of each
customer by 5000. Here, SQL%ROWCOUNT attribute is used to determine the number of
rows affected:

DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/

Output:
5 customers updated
PL/SQL procedure successfully completed.

Prepared By: Raju Poudel (Lecturer - MMC) 2


PL/SQL Explicit Cursors
The explicit cursors are defined by the programmers to gain more control over the context
area. These cursors 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.

Following is the syntax to create an explicit cursor:


CURSOR cursor_name IS select_statement;

Steps for using Explicit CURSOR in PL/SQL:


1. Declare the cursor to initialize in the memory.
2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.

1) Declare the cursor:


It defines the cursor with a name and the associated SELECT statement.
CURSOR name IS
SELECT statement;

2) Open the cursor:


It is used to allocate memory for the cursor and make it easy to fetch the rows returned by
the SQL statements into it.
OPEN cursor_name;

3) Fetch the cursor:


It is used to access one row at a time. You can fetch rows from the above-opened cursor as
follows:
FETCH cursor_name INTO variable_list;

4) Close the cursor:


It is used to release the allocated memory. The following syntax is used to close the above-
opened cursors.
Close cursor_name;

PL/SQL Explicit Cursor Example


Consider the following employee table with data:

Prepared By: Raju Poudel (Lecturer - MMC) 3


Output:
Statement processed.
Eid: 101 Name: Ram Salary: 25000
Eid: 102 Name: Shyam Salary: 55000
Eid: 103 Name: Gita Salary: 34000
Eid: 103 Name: Gita Salary: 34000

Fetching Cursor Into Row

Output:
Statement processed.
101 Ram 25000
102 Shyam 55000
103 Gita 34000

Prepared By: Raju Poudel (Lecturer - MMC) 4


PL/SQL Cursor FOR LOOP
The cursor FOR LOOP statement is an elegant extension of the numeric FOR LOOP
statement.
The numeric FOR LOOP executes the body of a loop once for every integer value in a specified
range. Similarly, the cursor FOR LOOP executes the body of the loop once for each row
returned by the query associated with the cursor.

A nice feature of the cursor FOR LOOP statement is that it allows you to fetch every row from
a cursor without manually managing the execution cycle i.e., OPEN, FETCH, and CLOSE.

The cursor FOR LOOP implicitly creates its loop index as a record variable with the row type
in which the cursor returns and then opens the cursor.

In each loop iteration, the cursor FOR LOOP statement fetches a row from the result set into
its loop index. If there is no row to fetch, the cursor FOR LOOP closes the cursor.

Syntax of the cursor FOR LOOP statement:


FOR record IN cursor_name
LOOP
process_record_statements;
END LOOP;

Where, record defines an individual row from cursor.

The following example declares an explicit cursor and uses it in the cursor FOR
LOOP statement.

Output:
Statement processed.
Eid: 101 Name: Ram
Eid: 102 Name: Shyam
Eid: 103 Name: Gita

Prepared By: Raju Poudel (Lecturer - MMC) 5


Cursor FOR LOOP with a SELECT statement
The following example is equivalent to the example above but uses a query in a cursor FOR
LOOP statement.

Output:
Statement processed.
Eid: 103 Name: Gita
Eid: 102 Name: Shyam
Eid: 101 Name: Ram

PL/SQL Cursor with Parameters


An explicit cursor may accept a list of parameters. Each time you open the cursor, you can
pass different arguments to the cursor, which results in different result sets.

Cursors with parameters are also known as parameterized cursors.

The following shows the syntax of a declaring a cursor with parameters:

CURSOR cursor_name (parameter_list)


IS
cursor_query;

To open a cursor with parameters, you use the following syntax:


OPEN cursor_name (value_list);

Prepared By: Raju Poudel (Lecturer - MMC) 6


Example of parameterized cursor:

Output:
Statement processed.
Eid: 102 Name: Shyam

Example of parameterized FOR LOOP Cursor:

Output:
Statement processed.
Name: Ram Salary: 25000
Name: Gita Salary: 34000

Prepared By: Raju Poudel (Lecturer - MMC) 7


PL/SQL parameterized cursor with default values
CURSOR cursor_name (
parameter_name datatype := default_value,
parameter_name datatype := default_value,
...
) IS
cursor_query;

Example is shown below:

Output:
Statement processed.
Eid: 102 Name: Shyam

Prepared By: Raju Poudel (Lecturer - MMC) 8


Nested Cursors
In PL/SQL we can create multiple cursors. We can open a cursor inside another cursor. So,
we can define nested cursor as a cursor that is opened and operated inside another cursor.

Nested cursor can be used to select data from multiple tables that are in a relationship with
each other. We can also use value accessed by outer cursor in inner cursor.

Syntax of Nested Cursor,

Example of Nested Cursor:


Consider following tables:

Employee Table

Department Table

Prepared By: Raju Poudel (Lecturer - MMC) 9


Output:
Statement processed.
Department: IT
Shyam 55000
Department: Finance
Ram 25000
Gita 34000

Prepared By: Raju Poudel (Lecturer - MMC) 10


Example of Nested Cursor inside FOR Loop

Output:
Statement processed.
Department: IT
Shyam 55000
Department: Finance
Ram 25000
Gita 34000

Prepared By: Raju Poudel (Lecturer - MMC) 11

You might also like