Exp.No:l9
Display Records Using Cursor
ise of Cursor
‘The major function of a cursor is to retrieve data, one row at a time, from a result
set, unlike the SQL commands which operate on all the rows in the result set at
one time, Cursors are used when the user needs to update records in a singleton
fashion or in a row by row manner, in a database table
‘The Data that is stored in the Cursor is called the Active Data Set, Oracle DBMS
has another predefined area in the main memory Set, within which the cursors
are opened. Hence the size of the cursor is limited by the size of this pre-
defined area.
‘ursor Actions
+ Declare Cursor: A cursor is declared by defining the SQL statement that returns 4
result set,
+ Open: A Cursor is opened and populated by executing the SQL.
statement defined by thecursor:
+ Fetch: When the cursor is opened, rows can be fetched from the
cursor one by one or in ablock to perform data manipulation.
+ Close: After data manipulation, close the cursor explicitly.
+ Deallocate: Finally, delete the cursor definition and release all the
system resourcesassociated with the cursor.
[Types of Cursors
sors are classified depending on the circumstances in which they are opened.
+ Implicit Cursor: If the Oracle engine opened a cursor for its internal
processing it is known as an Implicit Cursor. It is created
“automatically” for the user by Oracle when a query is executed and is
simpler to code.
+ Expl
Cursor: A Cursor can also be opened for processing data
through a PL/SQL block, on demand. Such a user-defined cursor is
known as an Explicit Cursor.
63CREATE TABLE CU
TOMERS WITH ID NAME,AGE,ADDRESS,SALARY
INSERT 5 RECORDS
™ NAT AGr ADDRESS: SATARY
1 Ramesh 23 allasabad zo)
2 Suresh 2 Kanpur
, Mahesh om eee) 27000
4 chanden 2 Neasa 25000
5 Ales oo Paris 2000
PLSQL CODE:
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 ¢ id customers.id%type:
3. c_name customers.name%type;
4 ¢_addr customers.address%type;
5 CURSOR c_customers is
6 SEL
"T id, name, address FROM customers;
7 BEGIN
8 OPEN c customers;
9 LOOP
10 FE
‘CH ¢_customers into ¢_id, ¢_name, ¢_addr;
11 EXIT WHEN c_customersY%notfound;
12 dbms_output.put_fine
cid
13 END LOOP;
14 CLOSE ¢ customers;
15 END; 16 /
64OUTPUT:
1 ramesh allhabad
2 suresh kanpur
3 mahesh kaja
4 chandan noida
5 alex paris
PL/SQL procedure successfully completed.
65