Lecture Notes Unit5 chapter19 Cursor in Pl/SQL

Murugan146644 953 views 14 slides Aug 14, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Description:
Welcome to the comprehensive guide on Relational Database Management System (RDBMS) concepts, tailored for final year B.Sc. Computer Science students affiliated with Alagappa University. This document covers fundamental principles and advanced topics in RDBMS, offering a structured appr...


Slide Content

RDBMS -Unit V
Chapter 19
Cursor
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
AlagappaGovernment Arts College, Karaikudi.
(Affiliated by AlagappaUniversity)
Mailid: [email protected]
Reference Book:
LEARN ORACLE 8i, JOSE A RAMALHO

Cursor -Definition
➢Acursorisatemporaryworkareacreatedinsystem
memorywhenaSQLstatementisexecuted.
➢Byusingcursormechanism,wecanreturnmultiple
rowsfromthetableusingselectstatement.
➢ForstaticSQLcommands,therearetwotypesof
cursors:implicitandexplicit.
➢OracleimplicitlydeclaresacursortoalltheDDLand
DMLcommandsthatreturnonlyonerow.
➢Forqueries(select)thatreturnmultiplerows,you
havetoexplicitlycreateacursor.

Creating a Cursor
Creating a cursor involves four steps:
1.Declaring the cursor—the cursor receives a name and
is assigned a SELECT command.
2. Opening the cursor—the query is executed and the
number of rows to be returned is determined.
3. Fetching—the row that is found is sent to the program
in PL/SQL.
4. Closing the cursor—the Oracle resources allocated to
the cursor are freed.

Declaring the Cursor
The DECLARE command is used to declare a cursor.
Syntax:
DECLARE
CURSOR cursor_name[(parameter[, parameter]...)]
[RETURN return_type] IS SELECT COMMAND;
Arguments:
cursor_name
Name of the cursor to be created.
return_type
Represents a row or record in a table.

Declaring the Cursor
Example 1:
DECLARE
CURSOR prim01 IS SELECT empno, ename, job,
salFROM empWHERE deptno=30;
Example 2:
DECLARE
CURSOR seg02 RETURN dept%ROWTYPEIS
SELECT * FROM deptWHERE deptno= 20;

Opening a Cursor
➢Theopeningofthecursoristheoperationthat
performsthequeryandcreatestheresultset,whichis
thegroupofrowsthatmeetsthequerycondition.
➢AcursorisopenedwiththeOPENcommand:
➢OPENprim01;

Attributes of an Explicit Cursor
➢Whenacursororvariableisopened,therowsthat
satisfythequeryareidentifiedandformtheresultset.
Therowsoftheresultsetaredownloadedoneata
time.
➢%FOUNDreturnstruewhenDMLcommandsaffects
oneormultiplerows.
➢%ISOPEN returns TRUE when the cursor or variable
is opened. Otherwise, it returns FALSE.
➢%NOTFOUND returns TRUE when the last row of
the cursor is processedand no other row is available.
➢%ROWCOUNT returns the total number of rows
returned by the FETCH command. Each time a
FETCH command is executed, %ROWCOUNT is
increased by 1.

Attributes of an Implicit Cursor
Attributes of an Implicit Cursor
The attributes of an implicit cursor return information
about the execution of the INSERT, UPDATE, DELETE,
or SELECT INTO commands.
Example:
DELETE FROM empWHERE empno= emp;
IF SQL%FOUND THEN —successful
INSERT INTO new_empVALUES (func, my_ename,
...);

Accessing the Cursor Rows
➢AfterthecursorisopenedwiththeOPENcommand,
theselectedrowsareavailable.
➢PL/SQLusestheFETCHcommandtoreadthe
contentsofarow.
➢FETCHreadsthevalueofeachcolumnoftherowthat
isspecifiedbytheSELECTcommand,andassignsa
variable.Thiscanbeseenasanequivalenttothe
SELECTINTOcommand.
➢TheFETCHcommandrecoversonerowatatime
fromtheresultset.

Accessing the Cursor Rows -example
LOOP
FETCH display30 INTO t_ename, t_deptno, t_sal;
EXIT WHEN display30%notfound;
DBMS_OUTPUT.PUT_LINE(t_ENAME// ´´
//TO_CHAR(T_DEPTNO) //
´´/ / TO_CHAR (T_SAL)) ;
END LOOP;

Closing a Cursor
➢Whenacursorisnotinuse,itmustbeclosedso
Oraclecanfreetheresourcesthatwereallocatedtoit.
➢ThecommandresponsibleforthistaskisCLOSE:
Example:
➢CLOSEdisplay30

Steps for executing a Cursor
1.CreateanEmployeetable
createtableempl(namevarchar2(25),dnonumber(5),
salarynumber(7,2));
2.Insertfewrecordsandviewtherecord
3.TypetheProgramandexecutetheprogram
sql>editd:\agac\cursor1.sql
sql>@d:\agac\cursor1.sql
4.Executetheprocedure
SQL>execcursor1

Example Program
CREATE OR REPLACE PROCEDURE CUR1 IS
e_nameempl.name%type;
e_dnoempl.dno%type;
e_salaryempl.salary%type;
CURSOR dispempIS
SELECT NAME,DNO,SALARY FROM EMPL WHERE DNO=1002
ORDER BY NAME;
BEGIN
DBMS_OUTPUT.ENABLE;
/*open cursor*/
OPEN dispemp;
LOOP
FETCH dispempINTO e_name, e_dno, e_salary;
EXIT WHEN dispemp%notfound;
DBMS_OUTPUT.PUT_LINE( e_name|| '-'||TO_CHAR(e_dno) || '-'
||TO_CHAR (e_salary)) ;
END LOOP;
close dispemp;
end;
/

Example Program
1. A procedure called Cursor1, which displays the contents of
the Ename, Deptno, and Salary fields of the EMPL table.
2.Initially, three variables are created. They will contain the
contents of the fields. To do that, use the %type attribute
that creates it with the same data type as the field.
3. Then, the cursor is defined with the name Dispempand the
specified SELECT command.
4. Activate the output of data from the DBMS_OUTPUT
package, and then open the cursor.
5.A loop is executed until there are no more rows to be
processed.
6. The FETCHcommanddownloads the contents of the fields
to the variables.
7.Finally, the procedure displays the data: