0% found this document useful (0 votes)
6 views7 pages

Notes Chapter 2.3 Lecture 2.3.4 (Cursors)

This document introduces PL/SQL cursors in Oracle databases, explaining their purpose in managing result sets from SQL queries. It details the two types of cursors: implicit and explicit, along with their functionalities and attributes. Examples are provided to illustrate how to declare, open, fetch, and close explicit cursors, as well as the use of implicit cursors in DML operations.

Uploaded by

Saloni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Notes Chapter 2.3 Lecture 2.3.4 (Cursors)

This document introduces PL/SQL cursors in Oracle databases, explaining their purpose in managing result sets from SQL queries. It details the two types of cursors: implicit and explicit, along with their functionalities and attributes. Examples are provided to illustrate how to declare, open, fetch, and close explicit cursors, as well as the use of implicit cursors in DML operations.

Uploaded by

Saloni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT 2

CHAPTER 2.2

Introduction to PL/SQL Cursor

When you work with Oracle database, you work with a complete set of rows returned from
an SELECT statement. However the application in some cases cannot work effectively with
the entire result set, therefore, the database server needs to provide a mechanism for the
application to work with one row or a subset of the result set at a time. As the result, Oracle
created PL/SQL cursor to provide these extensions.

A PL/SQL cursor is a pointer that points to the result set of an SQL query against database
tables.

Working with PL/SQL Cursor

The following picture describes steps that you need to follow when you work with a PL/SQL
cursor:

Fig 1: PL/SQL Cursor

Oracle has dedicated memory locations for executing SQL statements and then it holds that
processed information, for example, the total number of rows updated.

A cursor in PL/SQL gives a name and acts as a pointer to the area of work called a context
area and then uses its information. It keeps the number of rows processed by the SQL
statement. These rows are called as an active set. The size of the active set is equal to the
count of the rows that meet the condition.

There are two types of cursors which are listed below:


1. Implicit Cursor

2. Explicit 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 theSQL cursor, which always has attributes su

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

Example
We will be using the CUSTOMERS table

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 –

DECLARE

total_rows number(2);

BEGIN

UPDATE customers

SET salary = salary + 500;

IF sql%notfound THEN

dbms_output.put_line('no customers selected');

ELSIF sql%found THEN

total_rows := sql%rowcount;

dbms_output.put_line( total_rows || ' customers selected ');

END IF;

END;
/

When the above code is executed at the SQL prompt, it produces the following result

6 customers selected

PL/SQL procedure successfully completed.

If you check the records in customers table, you will find that the rows have been updated

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 –

CURSOR cursor_name IS select_statement;

Working with an explicit cursor includes the following 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

CURSOR c_customers IS

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

OPEN c_customers;

Fetching the Cursor

Fetching the cursor involves accessing one row at a time. For example, we will fetch rows from the above-op

FETCH c_customers INTO c_id, c_name, c_addr;

Closing the Cursor

Closing the cursor means releasing the allocated memory. For example, we will close the above-opened curs

CLOSE c_customers;

Example

Following is a complete example to illustrate the concepts of explicit cursors &minua;

DECLARE

c_id customers.id%type;

c_name customer.name%type;

c_addr customers.address%type;

CURSOR c_customers is

SELECT id, name, address FROM customers;


BEGIN

OPEN c_customers;

LOOP

FETCH c_customers into c_id, c_name, c_addr;

EXIT WHEN c_customers%notfound;

dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);

END LOOP;

CLOSE c_customers;

END;

When the above code is executed at the SQL prompt, it produces the following result

1 Ramesh Ahmedabad

2 Khilan Delhi

3 kaushik Kota

4 Chaitali Mumbai

5 Hardik Bhopal

6 Komal MP

PL/SQL procedure successfully completed.

Other References
● PL/SQL - Cursors - Tutorialspoint

● PL/SQL Cursor - javatpoint

● PL/SQL Cursor (plsqltutorial.com)

Suggested Book References

● C.J.Date, “An Introduction to DatabaseSystems”, Addison Wesley.

● Thomas M. Connolly, Carolyn & E.Begg,“Database Systems: A Practical Approach


to Design, Implementationand Management”, 5/E, University of Paisley, Addison-
Wesley.

● Rob,”Database Principal Fundamental Design, Cengage Learning.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy