BSC PLSQL Notes Unit 4
BSC PLSQL Notes Unit 4
CURSORS
WHAT IS CURSOR?
The Oracle engine uses a work area for its internal processing in order to
execute an SQL statement. This work area is private to SQL’s operations and is
called a Cursor.
The data that is stored in the cursor is called Active Data Set. Conceptually, the size of the
cursor in memory is the size required to hold the number of rows in the Active Data Set. The
actual size is determined by the oracle engines built in memory management capabilities and
the amount of RAM available.
The values retrieved from a table are held in a cursor opened in opened in memory by
the Oracle engine. This data is then transformed to the client machine via the network. In
order to hold this data, a cursor is opened at the client end.
TYPES OF CURSORS:
Cursors are classified depending on the circumstances under which they are opened. If
the oracle engine opened a cursor for its internal processing it is known as an Implicit
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.
When a oracle engine creates a Implicit and Explicit Cursor control variables are also created
to control the execution of the cursor. These cursor variables can be accessed and used in a
PL/SQL code block.
Both Implicit and Explicit Cursor have four attributes. They are described below:
1
UNIT-4
III Semester BCA
%NOTFOUND Returns TRUE if record was not fetched successfully, FALSE otherwise.
IMPLICIT CURSOR:
The oracle engine implicitly opens a cursor on the server to process each
SQL statement. Since the implicit cursor is opened and managed by the oracle
engine internally, the function of reserving an area in memory, populating this
area with appropriate data, processing the data in the memory area, releasing the
memory area when the processing is complete is taken care of by the oracle
engine.
Implicit cursor attributes can be used to access information about the status of the last
insert, update and delete or single-row select statement. This can be done by preceding the
implicit cursor attribute with the cursor name.
%ISOPEN The oracle engine automatically opens and closes the SQL cursor after executing
its associated select, insert, update or delete statement has been processed in case
of implicit cursor. Returns TRUE if cursor is open, FALSE otherwise.
%NOTFOUND Returns TRUE if record was not fetched successfully, FALSE otherwise.
%ROWCOUNT Returns a number of rows affected by an insert, update or delete or select into
statement.
2
UNIT-4
III Semester BCA
Example 1:
BEGIN
UPDATE Emp_mstr SET branchno = &branchno WHERE empno = &empno;
IF SQL%FOUND THEN
dbms_output.put_line(‘Employee successfully transferred’);
END IF;
IF SQL%NOTFOUND THEN
dbms_output.put_line(‘Employee number does not exists’);
END IF;
END;
Example 2:
DECLARE
Rows-affected char (4);
BEGIN
UPDATE Acc_mstr SET status = ‘A’ WHERE status = ‘S’ AND
Branchno IN (SELECT branchno FROM Branch_mstr WHERE name = ‘dhanya’);
Rows-affected := TO_CHAR (SQL%ROWCOUNT);
IF SQL%ROWCOUNT >0 THEN
dbms_output.put_line(rows-affected ||’accounts activated successfully’);
ELSE
dbms_output.put_line(‘currently there exists no inactive account’);
END IF;
END;
EXPLICIT CURSOR:
When individual records i a table have to be processed inside a PL/SQL code block a
cursor is used. This cursor will be declared and mapped to an SQL query in the declare
section of the PL/SQL block and used within its Executable Section. A cursor thus created
and used is known as an Explicit Cursor.
The steps involved in using an explicit cursor and manipulating data in its active set are:
Declare a cursor mapped to a SQL select statement that retrieves data for processing
3
UNIT-4
III Semester BCA
CURSOR DECLARATION:
A cursor is defined in the declarative part of a PL/SQL block. This is done by naming
the cursor and mapping it to a query. When a cursor is declared, the oracle engine is informed
that a cursor of the said name needs to be opened. The declaration is only intimation. There is
no memory allocation at this point in time. The three commands used to control the cursor
subsequently are open, fetch and close.
A fetch statement then moves the data held in the Active Data Set into memory
variables.
The fetch statement is placed inside a loop . . . End loop constructs, which
causes the data to be fetched into the memory variables and processed until all
the rows in the Active Data Set are processed. The fetch loop then exists. The
existing of the fetch loop is user controlled.
4
UNIT-4
III Semester BCA
After the fetch loop exits, the cursor must be closed with the close statement.
This will release the memory occupied by the cursor and its Active data set. The
cursor name is used to reference the active data set held within the cursor.
OPENING A CURSOR:
Opening a cursor executes the query and creates the active set that contains all rows,
which meets the query search criteria. An open statement retrieves records from a database
table and placed the records in the cursor. The cursor is opened in the server’s memory.
The fetch statement retrieves the rows from the active set opened in the server memory
variables declared in the PL/SQL code block on the client one row at a time. The memory
variables are opened on the client machine. Each time a fetch is executed, the cursor pointer
is advanced to the next row in the Active Data Set.
Syntax:
CLOSING A CURSOR:
The close statement disables the cursor and the active set becomes undefined. This will
release the memory occupied by the cursor and its data set both on the client and the server.
Syntax: CLOSE CursorName;
EXPLICIT CURSOR ATTRIBUTES:
%ISOPEN Evaluates to TRUE, if the explicit cursor is open; or to FALSE, if it is closed. The
5
UNIT-4
III Semester BCA
%FOUND Returns TRUE if record was fetched successfully, FALSE otherwise. The syntax for
accessing this attribute is CursorName%FOUND.
%NOTFOUND Returns TRUE if record was not fetched successfully, FALSE otherwise. The syntax
for accessing this attribute is CursorName%NOTFOUND.
%ROWCOUN Returns a number of rows affected by an insert, update or delete or select into
T statement. The syntax for accessing this attribute is CursorName%ROWCOUNT.
Example:DECLARE
CURSOR Crsr_NoTrans IS
SELECT ACCTNO, STATUS, OPNDT, TYPE FROM ACCT_MSTR
WHERE ACCTNO IN (SELECT ACCTNO FROM TRANS_MSTR
GROUP BY ACCTNO HAVING MAX (SYSDATE-DT) >365);
str_ACCTNO ACCT_MSTR.ACCTNO%type;
str_STATUS ACCT_MSTR.STATUS%type;
str_OPNDT ACCT_MSTR.OPNDT%type;
str_TYPE ACCT_MSTR.TYPE%type;
BEGIN
OPEN Crsr_NoTrans;
IF Crsr_NoTrans%ISOPEN THEN
LOOP
FETCH Crsr_NoTrans INTO str_ACCTNO, str_STATUS, dt_OPNDT,
str_TYPE;
EXIT WHEN Crsr_NoTrans%NOTFOUND;
IF Crsr_NoTrans%FOUND THEN
6
UNIT-4
III Semester BCA
PARAMETERIZED CURSORS:
Commercial applications require that the query, which defines the cursor, be generic
and the data that is retrieved from the table be allowed to changed according to
need.
Oracle recognizes this and permits the creation of parameterized cursors
for use. The contents of parameterized cursors will constantly change
depending upon the value passed to its parameter.
Since the cursor accepts user-defined values into its parameters, thus changing the result set
extracted, it is called as Parameterized Cursor.
7
UNIT-4
III Semester BCA
Example:
OPEN Crsr_Branch_Chk (str_BRANCHNAME);
ORACLE PACKAGES
A package is an oracle object, which holds other objects within it. Objects commonly
held within a package are procedures, functions, variables, constants, cursors and exceptions.
It is the way of creating generic, encapsulated, re-usable code.
A package once written and debugged is compiled and stored in oracle’s system
tables held in an oracle database. Packages can contain PL/SQL blocks of code,
which have been written to perform some process entirely on their own. These
PL/SQL blocks of code do not require any kind of input from other PL/SQL
blocks of code. These are packages standalone subprograms.
A package’s specification declares the types (variables of the Record type), memory
variables, constants, exceptions, cursors, and subprograms that are available for use.
A package body fully defines cursors, functions, and procedures and thus implements the
specification.
PACKAGE SPECIFICATION:
This means that procedures, functions, variables, constants, cursors and exceptions and other
objects, declared in a package are accessible from anywhere in the package.
8
UNIT-4
III Semester BCA
The body of the package contains the definition of public objects that are declared in
the specification. The body can also contain other object declarations that are private to the
package. The objects declared privately in the package body are not accessible to other
objects outside the package. Unlike package specification, the package body can contain
subprogram bodies.
CREATING PACKAGES:
The first step to creating a package is to create its specification. The specification
declares the objects that are contained in the body of the package. A package can include
functions and procedures. The variable declared within the package can be accessed by any
function or procedure within the package.
To create a package specification, the CREATE PACKAGE command:
Example:
After the specification is created, the body of the package needs to be created.
The body of the package is a collection of detailed definitions of the objects that were
declared in the specification.
These objects, or package subprograms, are accessible outside the package only if their
specifications are included in the package specifications.
If any initialization is done in the package body, it is executed once when the package is
initially referenced.
The following is an example of the body of the package that was specified in the
previous example’s specification:
Example:
CREATE OR REPLACE PACKAGE BODY pack1 IS
FUNCTION net(vempno IN number) RETURN number IS
netsalary number(10);
hra number(5);
basicsalary number(10);
9
UNIT-4
III Semester BCA
BEGIN
select basic, hra into basicsalary, hra from salary where eno = vempno;
netsalary:=(basicsalary*((hra*basicsalary)/100));
return netsalary;
END net;
PROCEDURE tax(vempno in number) IS
itax number(10);
netsalary number(10);
taxpaid number(10);
BEGIN
netsalary:=net(vempno);
taxpaid:=((itax*netsalary)/100);
dbms_ouput.put_line(‘the taxpaid=’||taxpaid);
END tax;
END pack1;
Syntax:
Example:
1. Each package is easily understood and the interfaces between packages are simple, clear
and well defined.
2. Packages allow granting of privileges efficiently.
10
UNIT-4
III Semester BCA
**************************************************************************
11