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

Cursors 1 Removed

The document explains how to create, compile, and execute PL/SQL Cursors and packages in a database. It details the two types of cursors: implicit and explicit, along with their attributes and examples of their usage. The document also provides step-by-step instructions for working with explicit cursors, including declaration, opening, fetching, and closing the cursor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cursors 1 Removed

The document explains how to create, compile, and execute PL/SQL Cursors and packages in a database. It details the two types of cursors: implicit and explicit, along with their attributes and examples of their usage. The document also provides step-by-step instructions for working with explicit cursors, including declaration, opening, fetching, and closing the cursor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Objective

• We will learn how to create, compile, and execute a PL/SQL Cursor


and package in Database.

Prof. Dileep Kumar Sahu, Assistant Professor


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:
1. Implicit Cursors
2. Explicit Cursors

Prof. Dileep Kumar Sahu, Assistant Professor


PL/SQL Cursor: 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 etc. are executed.
• Oracle 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.
Prof. Dileep Kumar Sahu, Assistant Professor
PL/SQL Cursor : Implicit Cursor
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.

Prof. Dileep Kumar Sahu, Assistant Professor


PL/SQL Cursor : Implicit Cursor - Example
• Create customers table and have records: Customer
• A Program to update the table and increase salary of each
customer by 5000. Id Name Department Salary
• Create procedure:
1 Ramesh web developer 45000
DECLARE
total_rows number(2); 2 Sohan program developer 55000
BEGIN
3 Mohan web designer 45000
UPDATE customers
SET salary = salary + 5000; Output: 3 Customers updated
IF sql%notfound THEN
After Execution of cursor the table will be updated
dbms_output.put_line('no customers updated');
as : Customer
ELSIF sql%found THEN
total_rows := sql%rowcount;
Id Name Department Salary
dbms_output.put_line( total_rows || ' Customers updated ');
END IF; 1 Ramesh web developer 55000
END;
2 Sohan program developer 65000
/
Here: SQL%ROWCOUNT attribute is used to determine the number of rows affected. 3 Mohan web designer 55000

Prof. Dileep Kumar Sahu, Assistant Professor


PL/SQL Cursor: Explicit Cursors

• The Explicit cursors are defined to get 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.
1. Declare the cursor:
• to initialize in the memory.
• It defines the cursor with a name and the associated SELECT statement.
• Syntax:
CURSOR name IS
SELECT statement;

Prof. Dileep Kumar Sahu, Assistant Professor


Steps to create Explicit Cursors
1. Declare the cursor:
• to initialize in the memory.
• It defines the cursor with a name and the associated SELECT statement.
• Syntax:
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.
Syntax for cursor open:
OPEN cursor_name;

Prof. Dileep Kumar Sahu, Assistant Professor


Steps to create Explicit Cursors

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:
Syntax for cursor fetch:
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.
Syntax for cursor close:
Close cursor_name;

Prof. Dileep Kumar Sahu, Assistant Professor


PL/SQL Cursor: Explicit Cursors - Example

• Explicit cursors are defined by programmers to gain more control over the
context area.
• It is defined in the declaration section of the PL/SQL block.
• It is created on a SELECT statement which returns more than one row.

Prof. Dileep Kumar Sahu, Assistant Professor


PL/SQL Cursor : Explicit Cursor - Example
• PL/SQL Program to retrieve the customer name and address. Customer
DECLARE
Id Name Address Salary
c_id customers.id%type;
c_name customers.name%type; 1 Ramesh Durg 45000
c_addr customers.address%type;
2 Sohan Bhilai 55000
CURSOR c_customers is
SELECT id, name, address FROM customers; 3 Mohan Raipur 45000
BEGIN
OPEN c_customers;
LOOP Output:
FETCH c_customers into c_id, c_name, c_addr;
1 Ramesh Durg
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr); 2 Sohan Bhilai
END LOOP; 3 Mohan Raipur
CLOSE c_customers;
END; PL/SQL procedure successfully completed.
/
Prof. Dileep Kumar Sahu, Assistant Professor

You might also like