RDBMSPRACTICAL - 15 ENROLLMENT NO:-226010307090
PRACTICAL NO: 15
Aim: Implement PL/SQL programs using Cursors.
Description:
Definition:
Cursor is an area in memory where the data stored which is required to execute sql
statement. It means it is the part of the memory.
Attributes of the Cursor:
Name of attribute Description
%ISOPEN If cursor is OPEN, it returns True. Otherwise False
%FOUND If record fetched successfully then returns True otherwise False
%NOTFOUND If record Not fetched successfully then returns True otherwise False
%ROWCOUNT It returns the number of Records processed by Cursor
Types of Cursor:
1. Implicit Cursor
Cursor is called implicit, if it is opened by Oracle itself to execute any SQL
statement.
Example: Convert Specified Branch name in Account Table in Upper case Letter using
cursor.
Solution:
Declare
Branch
2. Explicit Cursor:
Cursor is called explicit cursor, if it is Opened by user to process data throgh PL
SQL Block.
It is used when there is a need to process more than one record Individually.
Steps:
A.Y. DADABHAI TECHNICAL INSTITUTE KOSAMBA, SURAT Page 1
RDBMSPRACTICAL - 15 ENROLLMENT NO:-226010307090
1. Declaration of Cursor:
Syntax: CURSOR Cursorname IS SELECT ………….
Example: CURSOR cAcc IS SELECT ano, balance, bname from Account;
2. Open a Cursor:
By Opening Cursor, Allocation Memory, Execute the Select Statement, Create active data
set, Set Row pointer to the First Record in Active Data set.
Syntax: OPEN Cursorname;
Example: OPEN cAcc;
3. Fetching Data:
In this stage, fetch data from active dataset and store in given variables.
Data from single row fetched at a Time.
After fetch current record row pointer updated to the next row in active data set.
Variable should be compitible with the column specified.
Syntax: FETCH cursorname INTO variable1, variable2…..;
Example: FETCH cAcc INTO no, bal, branch;
4. Close cursor:
Cursor should be closed after processing data. By closing cursor memory released
memory allocated to the cursor.
Syntax: CLOSE cursorname;
Example: CLOSE cAcc;
Example of Implicit Cursor:
Aim: In account table, convert the specified branch in to Upper case Letter.
Solution:
DECLARE
CURSOR cacc IS SELECT ANO, BALANCE, BRANCH FROM ACCOUNT2290;
no [Link]%TYPE;
balance [Link]%TYPE;
branch [Link]%TYPE;
BEGIN
OPEN cacc;
IF cacc%ISOPEN THEN
LOOP
FETCH cacc INTO no, balance, branch;
EXIT WHEN cacc%NOTFOUND;
IF branch='VVN' THEN
INSERT INTO acc_vvn values(no, balance);
DELETE FROM ACCOUNT2290 WHERE BRANCH= 'VVN';
END IF;
END LOOP;
COMMIT;
ELSE
dbms_output.put_line('CURSOR CANNOT OPEN');
END IF;
END;
A.Y. DADABHAI TECHNICAL INSTITUTE KOSAMBA, SURAT Page 2
RDBMSPRACTICAL - 15 ENROLLMENT NO:-226010307090
/
OUTPUT:
To Check the Details of Both Tables these commands can be used.
BEFORE:
AFTER:
A.Y. DADABHAI TECHNICAL INSTITUTE KOSAMBA, SURAT Page 3
RDBMSPRACTICAL - 15 ENROLLMENT NO:-226010307090
Example of Explicit Cursor:
Aim: Transfer all accounts belongs to ‘vvn’ branch from account table into another table
‘acc_vvn’ having only two collumns acc_no and balance
Solution:
DECLARE
CURSOR cacc IS SELECT ANO, BALANCE, BNAME FROM ACCOUNT22090;
no [Link]%TYPE;
balance [Link]%TYPE;
branch [Link]%TYPE;
BEGIN
OPEN cacc;
IF cacc%ISOPEN THEN
LOOP
FETCH cacc INTO no, balance, branch;
EXIT WHEN cacc%NOTFOUND;
IF branch = 'ksad' THEN
INSERT INTO acc_ksad values(no, balance);
DELETE FROM ACCOUNT22090s WHERE BNAME= 'ksad';
END IF;
END LOOP;
COMMIT;
ELSE
dbms_output.put_line('CURSOR CANNOT OPEN');
END IF;
END;
/
OUTPUT:
A.Y. DADABHAI TECHNICAL INSTITUTE KOSAMBA, SURAT Page 4
RDBMSPRACTICAL - 15 ENROLLMENT NO:-226010307090
BEFORE:
AFTER:
Progressive Assessment Sheet as per Rubrics
Regular Presentation Level of
Attendance Total
Assessment Understanding
05 05 07 08 25
Sign: Date:
A.Y. DADABHAI TECHNICAL INSTITUTE KOSAMBA, SURAT Page 5