PL SQL ASSIGNMENT - 4 Solution
PL SQL ASSIGNMENT - 4 Solution
declare
declare
cursor c_exam is select * from
exam where rollno = &rollno;
begin
for m_exam in c_exam
loop
dbms_output.put_line(
m_exam.rollno || ' ' || m_exam.paperno || ' ' ||
m_exam.marks);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is alredy opened ');
end;
/
declare
cursor c_acc is select * from
account where no = &ano;
Page 2
PL SQL ASSIGNMENT-4
begin
for m_acc in c_acc
loop
dbms_output.put_line(
m_acc.no || ' ' || m_acc.name || ' ' || m_acc.dob || ' ' ||
m_acc.op_date || ' ' || m_acc.type || ' ' || m_acc.status
|| ' ' || m_acc.flag || ' ' || m_acc.gender ||
m_acc.address || ' ' || m_acc.city || ' ' || m_acc.bal);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is alredy opened ');
end;
/
declare
cursor c_stud is select * from
student where to_char(sysdate,'yyyy') -
to_char(dob,'yyyy') >= 18;
begin
Page 3
PL SQL ASSIGNMENT-4
for m_stud in c_stud
loop
declare
cursor cp_std is select * from
student where rollno in (select rollno from fees);
cursor cnp_std is select * from
student where rollno not in (select rollno from fees);
begin
dbms_output.put_line(' list of
Page 4
PL SQL ASSIGNMENT-4
student who payed fees ');
for m_std in cp_std
loop
dbms_output.put_line('==============================
============');
dbms_output.put_line(' list of
student who not payed fees ');
for m_std in cnp_std
loop
declare
cursor c_s1(rn exam.rollno%type) is
select * from exam where paperno = 101 and rollno = rn;
-- paper no 101
cursor c_s2(rn exam.rollno%type) is
select * from exam where paperno = 102 and rollno =
rn; -- paper no 102
cursor c_s3(rn exam.rollno%type) is
select * from exam where paperno = 103 and rollno =
rn; -- paper no 103
cursor c_std is select * from
student;
tot number(10);
per number(10,2);
begin
for m_std in c_std
loop
Page 6
PL SQL ASSIGNMENT-4
dbms_output.put_line('
rollno : ' || m_std.rollno);
dbms_output.put_line('
name : ' || m_std.name);
-- paper no 101
for m_m1 in
c_s1(m_std.rollno)
loop
dbms_output.put_line('______________________________
____');
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is already opened ' );
end;
/
declare
cursor c_std is select * from
student;
tot number(10);
per number(10,2);
begin
for m_std in c_std
loop
dbms_output.put_line('
rollno : ' || m_std.rollno);
dbms_output.put_line('
name : ' || m_std.name);
dbms_output.put_line('______________________________
____');
exception
when cursor_already_open then
dbms_output.put_line('
cursor is already opened ' );
end;
/
====================================================
============================
Name of Account holder, Transaction Date,
Transaction Amount, Transaction Type
---
---- -----
-----
Page 10
PL SQL ASSIGNMENT-4
====================================================
============================
declare
cursor c_bran is select * from bran;
cursor c_t(bno
tran.bran_no%type) is select * from tran where bran_no
= bno;
cursor c_name(ano
account.no%type) is select name from account where
no = ano;
begin
for m_bran in c_bran
loop
dbms_output.put_line('----------------------------------
-------------');
dbms_output.put_line('name tran_date
tran_amount tran_type');
Page 11
PL SQL ASSIGNMENT-4
for m_t in
c_t(m_bran.b_no)
loop
for m_name
in c_name(m_t.no)
loop
end if;
end loop;
end loop;
dbms_output.put_line('==============================
=================');
dbms_output.put_line('-
-');
Page 12
PL SQL ASSIGNMENT-4
end loop;
end;
/
Gender : Male
====================================================
=====================
Account number Name account
opening date account balance
---- ---
---
---
====================================================
=====================
total male account holder
-----------------------------------------------------------
Page 13
PL SQL ASSIGNMENT-4
--------------
Gender : female
====================================================
=====================
Account number Name account
opening date account balance
---- ---
---
---
====================================================
=====================
total female account holder
declare
cursor cm_acc is select * from
account where gender = 'm';
cursor cf_acc is select * from
account where gender = 'f';
cnt_m number;
cnt_f number;
begin
dbms_output.put_line('Gender :
Page 14
PL SQL ASSIGNMENT-4
Male');
dbms_output.put_line('==============================
======================');
dbms_output.put_line('acc_no
name op_date balance');
dbms_output.put_line('-------
-------- ----------- --------- ');
for m_m in cm_acc
loop
dbms_output.put_line('----------------------------------
------------------');
dbms_output.put_line('total male
account holder : ' || cnt_m);
dbms_output.put_line('=
=');
dbms_output.put_line('Gender :
Page 15
PL SQL ASSIGNMENT-4
female');
dbms_output.put_line('==============================
======================');
dbms_output.put_line('acc_no
name op_date balance');
dbms_output.put_line('-------
-------- ----------- --------- ');
for m_m in cf_acc
loop
dbms_output.put_line('----------------------------------
------------------');
dbms_output.put_line('total
female account holder : ' || cnt_f);
end;
/
Page 16
PL SQL ASSIGNMENT-4
10. Write PL/SQL Block to perform transaction in bank
and update balance accordingly. While
performing withdraw transaction , check
that sufficient balance is available or not , if no
sufficient balance than display appropriate
message if balance is available then perform
transaction. Maintain minimum balance
500.
declare
acno account.no%type := &accno;
amt account.bal%type :=
&amount;
mbal account.bal%type;
a_bal account.bal%type;
begin
select bal into mbal from account
where no = acno;
if (amt < 0 ) then
if((mbal-amt) > 500)
then
update
account set bal = mbal + amt where no = acno;
Page 17
PL SQL ASSIGNMENT-4
dbms_output.put_line(' withdraw success ');
else
else
a_bal := mbal + amt;
update account set bal
= a_bal where no = acno;
dbms_output.put_line('
deposite transaction success');
end if;
end;
/
-- performing
-- creating table
for c in c_detail
loop
exp_dt := c.expire_date;
if (to_char(exp_dt,'dd')
>= to_char(sysdate,'dd') and to_char(exp_dt,'mon') >=
to_char(sysdate,'mon') and to_char(exp_dt,'yyyy') >=
to_char(sysdate,'yyyy') ) then
update
master set stock = (stock + c.units) where item_no =
c.item_no;
else
update
master set Unit_expired = (Unit_expired + c.units) where
item_no = c.item_no;
end if;
end loop;
end;
Page 22
PL SQL ASSIGNMENT-4
/
dbms_output.put_line('==============================
================');
for i in c_master
loop
dbms_output.put_line('==============================
Page 23
PL SQL ASSIGNMENT-4
================');
cnt_unit := (cnt_unit +
i.stock);
cnt_unit_expired :=
(cnt_unit_expired + i.unit_expired);
end loop;
dbms_output.put_line('grand total
' || cnt_unit || ' ' || cnt_unit_expired || ' ' ||
(cnt_unit + cnt_unit_expired));
exception
when no_data_found then
dbms_output.put_line('
invalid item no ');
end;
/
====================================================
====================
Total No Of Books Read = ___________ Total
Penalty
declare
cursor c_acc is select * from
account;
cnt_m number:= 0;
cnt number := 0;
cnt_s number := 0;
cnt_c number := 0;
cnt_o number := 0;
cnt_clo number := 0;
begin
for m_acc in c_acc
loop
dbms_output.put_line(
m_acc.no || ' ' || m_acc.name || ' ' || m_acc.dob || ' ' ||
m_acc.op_date || ' ' || m_acc.gender || ' ' || m_acc.city
|| ' ' || m_acc.bal || ' ' || m_acc.type);
if(m_acc.gender = 'm')
then
cnt_m :=
cnt_m + 1;
end if;
if(m_acc.type = 's') then
Page 26
PL SQL ASSIGNMENT-4
cnt_s := cnt_s +
1;
else
cnt_c := cnt_c
+ 1;
end if;
if (m_acc.status = 'o')
then
cnt_o := cnt_o
+ 1;
else
cnt_clo :=
cnt_clo + 1;
end if;
cnt := cnt + 1;
end loop;
dbms_output.put_line('
====================================================
============== ');
dbms_output.put_line('
total account holder = ' || cnt);
dbms_output.put_line('
total male account holder = ' || cnt_m);
dbms_output.put_line('
Page 27
PL SQL ASSIGNMENT-4
total saving account holder = ' || cnt_s);
dbms_output.put_line('
total current account holder = ' || cnt_c);
dbms_output.put_line('
total open account holder = ' || cnt_o);
dbms_output.put_line('
total close account holder = ' || cnt_clo);
end;
/
Publisher name
====================================================
===========
Details of book published by publisher
declare
cursor c_pub is select
pub_code,name from publisher;
cursor c_book(pb_cd
publisher.pub_code%type) is select * from book where
Page 28
PL SQL ASSIGNMENT-4
pub_code = pb_cd;
begin
for m_pub in c_pub
loop
dbms_output.put_line(' name : '
|| m_pub.name);
dbms_output.put_line('==============================
====================================');
for m_book in
c_book(m_pub.pub_code)
loop
end loop;
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
Page 29
PL SQL ASSIGNMENT-4
cursor is already opened ');
end;
/
declare
cursor c_book is select * from book
where pub_code = &pub_code;
begin
for m_book in c_book
loop
Page 31