0% found this document useful (0 votes)
22 views14 pages

Cursors

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 14

Cursors

Dr.P.Dhana Lakshmi
• 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:
• Implicit Cursors
• 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 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. 
• update the table and increase salary of each customer by 5000.
• Output:
6 customers updated
PL/SQL procedure successfully completed.
select * from customers;  
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.
• Syntax of explicit cursor
• CURSOR cursor_name IS select_statement;;  
Steps while working with an explicit cursor:
• Declare the cursor to initialize in the memory.
• Open the cursor to allocate memory.
• Fetch the cursor to retrieve data.
• Close the cursor to release allocated memory.
Declare the cursor:
• It defines the cursor with a name and the associated SELECT
statement.
• Syntax for explicit cursor decleration
• CURSOR name IS  
•  SELECT statement; 
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;  
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;  
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;  
PL/SQL Explicit Cursor 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.
• Let's take an example to demonstrate the use of explicit cursor. In
this example, we are using the already created CUSTOMERS table.
• 1 Ramesh Allahabad
• 2 Suresh Kanpur
• 3 Mahesh Ghaziabad
• 4 Chandan Noida
• 5 Alex Paris
• 6 Sunita Delhi
• PL/SQL procedure successfully completed.

You might also like