RDBMS PR 15
RDBMS PR 15
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.
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:
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.
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.
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 account2290.ano%TYPE;
balance account2290.balance%TYPE;
branch account2290.BRANCH%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;
/
OUTPUT:
BEFORE:
AFTER:
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 account22090.ano%TYPE;
balance account22090.balance%TYPE;
branch account22090.BNAME%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:
BEFORE:
AFTER:
05 05 07 08 25
Sign: Date: