PL SQL ASSIGNMENT-5
procedure & function related assignment
-- create procedure to display name
create or replace procedure pro_stud(mroll
in number)
is
mname student.name%type;
begin
select name into mname from
student where rollno = mroll;
dbms_output.put_line(mname);
end;
/
-- create procedure to display total marks
create or replace procedure
pro_tot_mark(mroll in number, tot_marks out
number)
is
begin
select sum(marks) into tot_marks
from exam where rollno = mroll;
end;
/
-- using function
declare
Page 1
PL SQL ASSIGNMENT-5
mrollno number(3);
mtot number(3);
begin
mrollno := &rollno;
pro_stud(mrollno);
pro_tot_mark(mrollno,mtot);
dbms_output.put_line('Total :' ||
mtot);
end;
/
create or replace function fun_tot(n1 in
number,n2 in number)
return number is
begin
return n1+n2;
end;
/
1. Create procedure to perform addition of two number
, pass number as parameter.
Page 2
PL SQL ASSIGNMENT-5
create or replace procedure pro_que1(n1 in
number,n2 in number)
is
add number(3);
begin
add := n1 + n2;
dbms_output.put_line('addition is
= ' || add);
end;
/
-- fror execute procedure
begin
pro_que1(&a, &b);
end;
/
2. Write pl-sql block to swap two number. use
procedure to perform swaping of two number.
create or replace procedure pro_que2(x in
Page 3
PL SQL ASSIGNMENT-5
number,y in number)
is
a number(3) := x;
b number(3) := y;
begin
dbms_output.put_line(' before
swaping value of A = ' || a );
dbms_output.put_line(' before
swaping value of B = ' || b );
a := a + b;
b := a - b;
a := a - b;
dbms_output.put_line(' afrer
swaping value of A = ' || a );
dbms_output.put_line(' afrer
swaping value of B = ' || b );
end;
/
-- fror execute procedure
begin
pro_que2(&a,&b);
Page 4
PL SQL ASSIGNMENT-5
end;
/
3. Write pl-sql block to display the result of student for
given rollno. create separation
function to calculate the percentage of student , pass
rollno of student and return
percentage to host environment.
create or replace procedure pro_que3(mroll
in student.rollno%type)
is
mtot exam.marks%type;
mper number(10,2);
begin
select sum(marks) into mtot from
exam where rollno = mroll;
mper := mtot / 3;
dbms_output.put_line('percentage
= ' || mper);
end;
/
Page 5
PL SQL ASSIGNMENT-5
-- for execute this procedure
begin
pro_que3(&roll);
end;
/
4. Create function to know the balance of given
account holder
create or replace function fun_bal(ano in
number)
return number is
mbal number(10);
begin
select bal into mbal from account
where no = ano;
return mbal;
end;
/
Page 6
PL SQL ASSIGNMENT-5
select fun_bal(&ano) from dual;
5. Write pl-sql block to perform banK transaction.
1. Create function chk_balance to
know that balance of given account holder.
2. Write procedure to
update_balance to update the balance of given
holder.
-- function for know balance of gicen account holder
create or replace function chk_bal(ano in
number)
return number
is
mbal number;
begin
select bal into mbal from account
where no = ano;
return mbal;
end;
/
-- procedure for update balance of account holder
Page 7
PL SQL ASSIGNMENT-5
create or replace procedure update_bal(ano
in number,mbal in number,b in number)
is
balance number := b;
begin
if(balance >= 0)then
update account set bal
= (mbal + b) where no = ano;
dbms_output.put_line('deposite success');
else
update account set bal
= (mbal + b) where no = ano;
dbms_output.put_line('withdraw success');
end if;
end;
/
+
-- PL/SQL block for perfrom transaction.
declare
ano number := &ano;
b number := &balance;
balance number;
Page 8
PL SQL ASSIGNMENT-5
begin
balance := chk_bal(ano);
update_bal(ano,balance,b);
commit;
end;
/
6. Write plslq blokc to display book title and status of
all book. Create function
to know the status of book based on given
accno. function should return a status
of book.
-- function for know status of book of given accno.
create or replace function f_status(ano in
number)
return number is
mstatus book.status%type;
begin
select status into mstatus from
book where accno = ano;
return mstatus;
end;
Page 9
PL SQL ASSIGNMENT-5
/
-- PL/SQL block to select book title and status of all
book
declare
ano book.accno%type:=
&book_accno;
mtitle book.title%type;
mstatus book.status%type;
begin
select title into mtitle from book
where accno = ano;
mstatus := f_status(ano);
dbms_output.put_line(' title
status ');
dbms_output.put_line(mtitle || '
' || mstatus);
end;
/
7. Write pl-sql block to perform book transaction
(issue/return). To perform book
transaction enter book accno , rollno and
Page 10
PL SQL ASSIGNMENT-5
transaction type(I-Issue,R-Return).
1. If transaction is issue then check
where book is available or not
(use function create in ex-6)if
book is not available raise user define
exception Book_not_available.
2. Before issuing book to student
check that student have book? If student
have book then raise user define
exception student_have_book.
3. Create separate function to
know that student have book or not
pass rollno as parameter.
4. IF book is available and student
have no book then perform book
transaction and update data
accordingly
-- uncompleted...........
create or replace function f_check_book(roll in
number)
return number
is
Page 11
PL SQL ASSIGNMENT-5
mroll stud.rollno%type;
begin
select rollno into mroll from book_tran where
rollno = roll group by rollno having count(iss_date) -
count(ret_date) = 0;
return mroll;
end;
/
declare
Book_not_available exception; --
user define exception for book not available.
student_have_book exception; --
user define exception for student have book.
no book.accno%type:=
&book_accno;
roll stud.rollno%type := &rollno;
tran_type char(1) := '&tran_type';
mt_id book_tran.tranid%type;
begin
Page 12
PL SQL ASSIGNMENT-5
if(tran_type = 'i') then
if(f_status(no) = 2) then
if(f_check_book(roll) = roll) then
--
inserting part
select max(tranid) into mt_id from book_tran;
mt_id := mt_id + 1;
dbms_output.put_line(' ----------------------------
book is issued by rollno : ' || roll || '
---------------------------- ');
insert into book_tran(tranid,accno,rollno,iss_date)
values(mt_id,no,roll,sysdate);
--
updating part
update book set status = 1 where accno = no;
dbms_output.put_line(' ----------------------------
Page 13
PL SQL ASSIGNMENT-5
------------------------------------------
---------------------------- ');
else
raise student_have_book;
end if;
else
raise
Book_not_available;
end if;
elsif(tran_type = 'r') then
dbms_output.put_line('
this is return book block ');
else
dbms_output.put_line('
enter prorper operation value ');
dbms_output.put_line('
like -> i for issue ');
dbms_output.put_line('
and -> r for return ' );
end if;
exception
when Book_not_available then
dbms_output.put_line('
Page 14
PL SQL ASSIGNMENT-5
book is not available ');
when student_have_book then
dbms_output.put_line('
student have book ');
end;
/
8. Create function to know the fees paid by student,
pass appropriate parameter
and return fees paid by student.
create or replace function f_check_fess(no in
number)
return number is
mfee fees.fees_amount%type;
begin
select fees_amount into mfee from
fees where rollno = no;
return mfee;
end;
/
select f_check_fess(&no) from dual;
Page 15
PL SQL ASSIGNMENT-5
9. Write pl/sql block to display name and age of all
students. Create separate
procedure to pass birth date as in parameter and
use age as out parameter to
get back the value of age from procedure
create or replace procedure p_find_age(dt in
date,age out number)
is
begin
age := to_char(sysdate,'yyyy') -
to_char(dt,'yyyy');
dbms_output.put_line('age := ' ||
age);
end;
/
declare
age number(3);
cursor c_stud is select name,dob from
student;
begin
Page 16
PL SQL ASSIGNMENT-5
for m_stud in c_stud
loop
dbms_output.put_line('name := ' || m_stud.name);
p_find_age(m_stud.dob,age);
end loop;
end;
/
10.Create function to know the marks of given student
in given subject. Pass rollno
and paperno as an argument and return marks
create or replace function f_find_mark(roll in
number,pap_no in number)
return number
is
mmark exam.marks%type;
begin
select marks into mmark from
Page 17
PL SQL ASSIGNMENT-5
exam where rollno = roll and paperno = pap_no;
return mmark;
end;
/
select f_find_mark(&rollno,&pap_no) from dual;
==============================================
=========== TRIGGER ==========
=============================================
1. Write database trigger to restrict user to perform
operation on student table on Sunday.
create or replace trigger tre_q1 before insert
or update or delete on student
for each row
declare
d char(3);
begin
d := to_char(sysdate,'dy');
if (d in ('sun','set')) then
Page 18
PL SQL ASSIGNMENT-5
raise_application_error(-20001,'not allow to perform
operation on student table today');
end if;
end;
/
2. Create table data _log(userid char(30),opdate
date, trtype char(10)). To maintain log of
operation perform on transaction.( Trtype
can be insert,update or delete)
Write data base trigger to maintain log of
operation performed on transaction table. If student
perform any operation on transaction table
then record it details in above given table.
-------pendinggggggg-------
3. Write data base trigger to perform following task
on transaction table.
1. While user perform withdraw operation ,
check balance value, if insufficient balance then
raise user define exception NO_Balance.
2. While user perform any transaction
update balance accordingly.
Page 19
PL SQL ASSIGNMENT-5
create or replace trigger tri_operation_tran before
insert or update on tran
for each row
declare
no_balance exception;
ano number(3);
mbal munber(10);
ta numner(10);
begin
if inserting then
ano := (:new.no);
ta := (:new.
elsif updating then
ano := (:old.no);
else
dbms_output.put_line(' delete
success fully ');
end if;
exception
when no_balance then
dbms_output.put_line(' your
balance insufficient');
end;
Page 20
PL SQL ASSIGNMENT-5
/
4. Write data base trigger to restrict user to change
rollno of student.
-- not complete --
create or replace trigger tir_change_roll
before update on student
for each row
declare
roll_old number(3);
roll_new number(3);
begin
roll_old := (:old.rollno);
roll_new := (:new.rollno);
if (roll_old <> roll_new) then
raise_application_error(-20001,' you can not change
your rollno ');
end if;
end;
/
5. Write database trigger to perform following task
Page 21
PL SQL ASSIGNMENT-5
while student issue a particular book.
1. To check book Is available or not if not
available raise appropriate exception.
2. To check that student have a book or not ,
if student have book then reject issue operation.
3. While issuing book update books and
student status accordingly.
create or replace tirgger tir_iss_book before
insert on book_tran
for each row
declare
ano number(3);
roll number(10);
mroll number(10);
exception
begin
roll := (:new.rollno);
select accno into ano from
book_tran where rollno = roll group by accno having
count(iss_date)-count(ret_date)>0;
select rollno into mroll from
book_tran where rollno = rno group by rollno having
count(iss_date)-count(ret_date)>0;
Page 22
PL SQL ASSIGNMENT-5
-*
exception
end;
/
6. Write database trigger to restrict user to insert data
into account table if age of account holder
is less than 18 years.
create or replace trigger tri_acc before insert
on account
for each row
declare
age number(3);
begin
age := to_char(sysdate,'yyyy') -
to_char(:new.dob,'yyyy');
if (age < 18) then
raise_application_error(-20001,' your age not grater
then 18');
end if;
end;
Page 23
PL SQL ASSIGNMENT-5
/
7. Write database trigger not allow marks more than
100 in result table.
create or replace trigger tri_exam
before insert or update on exam
for each row
declare
mark number(3);
begin
mark := (:new.marks);
if (mark > 100) then
raise_application_error(-20001,'invalid marks');
end if;
end;
/
8. Write database trigger to restrict user to perform
any transaction if status of account is close.
Page 24
PL SQL ASSIGNMENT-5
create or replace trigger tri_tran before
insert on tran
for each row
declare
mno number(3);
mstatus char(1);
begin
mno := (:new.no);
select status into mstatus from
account where no = mno;
if(mstatus = 'c') then
raise_application_error(-20001,'your account is closed');
end if;
end;
/
9. Write database trigger to restrict student to
perform any book transaction if account of student
is lock.
create or replace trigger tri_bt before insert
on book_tran
for each row
Page 25
PL SQL ASSIGNMENT-5
declare
mroll number(10);
mstatus number(2);
begin
mroll := (:new.rollno);
select status into mstatus from
stud where rollno = mroll;
if(mstatus = 1) then
raise_application_error(-20001,' your account is close');
end if;
end;
/
10. Write database trigger to restrict user to enter
result of student who has paid fees less than
5000.
create or replace trigger tri_less_fees before
insert on exam
for each row
declare
mroll number(3);
mfees number(10);
Page 26
PL SQL ASSIGNMENT-5
begin
mroll := (:new.rollno);
select fees_amount into mfees
from fees where rollno = mroll;
if( mfees < 5000) then
raise_application_error(-20001,'student no pay full
fees');
end if;
end;
/
Page 27