Cursor
Cursor is a mechanism that provides a way to select multiple rows of data from the database and then process each row individually inside a PL/SQL program.
There are two types of cursors
Implicit cursor- PL/SQL declare implicit cursor automatically for all SQL DML (Data Manipulation Language) statements. Including queries that return only one row.
Explicit cursor- Explicit cursor can be declared by the user and is used for queries that return more than one row.
In order to use explicit cursor we need to perform the following 5 steps:
1. Declare the cursor- We can declare a cursor using the following
syntax
Cursor <cursor_name> is
select statement;
e.g.
Cursor empcur is
select * from emp;
2. Declare a record to hold row from the table- We can declare a record to hold row form the table using the following syntax
<Record_name> <table_name>%rowtype;
e.g.
emprec emp%rowtype;
3. Open the cursor- We can open declared cursor using the following syntax
Open <cursor_name>;
e.g.
Open empcur;
4. Fetch rows from the cursor- We can fetch row using the following syntax
Fetch <cursor_name> into <record_name;
e.g.
Fetch empcur into emprec;
5. Close the cursor- We can close a cursor using the following syntax
Close <cursor_name>;
e.g.
Close empcur;
There are two types of cursors
Implicit cursor- PL/SQL declare implicit cursor automatically for all SQL DML (Data Manipulation Language) statements. Including queries that return only one row.
Explicit cursor- Explicit cursor can be declared by the user and is used for queries that return more than one row.
In order to use explicit cursor we need to perform the following 5 steps:
1. Declare the cursor- We can declare a cursor using the following
syntax
Cursor <cursor_name> is
select statement;
e.g.
Cursor empcur is
select * from emp;
2. Declare a record to hold row from the table- We can declare a record to hold row form the table using the following syntax
<Record_name> <table_name>%rowtype;
e.g.
emprec emp%rowtype;
3. Open the cursor- We can open declared cursor using the following syntax
Open <cursor_name>;
e.g.
Open empcur;
4. Fetch rows from the cursor- We can fetch row using the following syntax
Fetch <cursor_name> into <record_name;
e.g.
Fetch empcur into emprec;
5. Close the cursor- We can close a cursor using the following syntax
Close <cursor_name>;
e.g.
Close empcur;
UNIT-306
PL/SQL