Assignment 4
Assignment 4
: 4
1. Consider table Stud(Roll,Att,Status)
Write a PL/SQL block for following requirement and handle the exceptions.Roll no.
of student will be entered by user. Attendance of roll no. entered by user will be
checked in Stud table. If attendance is less than 75% then display the message
“Term not granted” and set the statusinstudtableas“D”.Otherwise display message
“Termgranted and set the status in stud table as “ND”.
Table created.
insert all
into Stud values(1,70,null)
into Stud values(2,90,null)
into Stud values(3,40,null)
into Stud values(4,68,null)
SELECT * FROM DUAL;
4 rows created.
Declare
mroll number(10);
matt number(10);
Begin
mroll:=&mroll;
select att into matt from Stud where roll=mroll;
if matt<75 then
dbms_output.put_line(mroll||'is detained');
update Stud set status='D' where roll=mroll;
else
dbms_output.put_line(mroll||'is Not detained');
update Stud set status='ND' where
roll=mroll;
end if;
Exception
when no_data_found then
dbms_output.put_line(mroll||'Notfound');
End;
/
Enter value for mroll: 1
old 5: mroll:=&mroll;
new 5: mroll:=1;
2. Write a PL/SQL block for following requirement using user defined exception
handling.The account_master table records the current balance for an account, which
is updated whenever, any deposits or withdrawals takes place. If the withdrawal
attempted is more than the current balance held in the account. The user defined
exception is raised, displaying an appropriate message. Write a PL/SQL block for
above requirement using user defined exception handling.
Table created.
insert all
into acc values(100,1000,null)
into acc values(200,10000,null)
into acc values(300,5000,null)
into acc values(400,7000,null)
SELECT * FROM DUAL;
4 rows created.
Declare
mbal number(10);
macc number(4);
trans_amt number(10);
No_sufficient_bal exception;
Begin
macc:=&acc_no;
trans_amt:=&trans_amt;
select balance into mbal from acc where acc_no = macc;
if (trans_amt<=mbal) then
update acc set balance=(balance-trans_amt) where acc_no=macc;
update acc set status='successful' where acc_no=macc;
else
raise No_sufficient_bal;
update acc set status='not success' where acc_no=macc;
end if;
Exception
End;
/
Enter value for acc_no: 100
old 10: macc:=&acc_no;
new 10: macc:=100;
Enter value for trans_amt: 100
old 11: trans_amt:=&trans_amt;
new 11: trans_amt:=100;
3. Write an SQL code block these raise a user defined exception where business rule
is voilated. BR for client_master table specifies when the value of bal_due field
is less than 0 handle the exception.
4.aBorrower(Roll_no,Name,DateofIssue,NameofBook,Status)
b.Fine(Roll_no,Date,Amt)
⦁ Accept roll_no &name of book from user.
⦁ Check the number of days(from date of issue),if days are between 15 to 30
then fine amount will be Rs 5per day.
⦁ If no. of days>30, per day fine will be Rs50 per day & for days less than 30,
Rs.5per day.
⦁ After submitting the book, status will change from I to R.
⦁ If condition of fine is true, then details will be stored into fine table.
Also handles the exception by named exception handler or user define exception
handler.
SOLUTION:
create table Borrowerx(
roll_no number,
dateofissue date,
nameofBook varchar(10),
status varchar(5));
Table created.
insert all
into borrowerx values(1,'1-Dec-23','ABC',null)
into borrowerx values(2,'8-Dec-23' ,'ABC',null)
into borrowerx values(3,'10-jan-24','ABC',null)
into borrowerx values(4,'10-feb-24','ABC',null)
SELECT * FROM DUAL;
4 rows created.
select * from borrowerx;
Declare
mroll number(5);
mbook varchar(10);
mdate date;
mfine number(10);
datediff number(10);
Begin
mroll:=&mroll;
mbook:='&mbook';
select dateofissue into mdate from borrowerx where roll_no=mroll and
nameofbook=mbook;
datediff:=to_date(sysdate)-to_date(mdate);
INSERT ALL
into fine values(1,'10-Jan-24',100)
into fine values(2,'22-Dec-23',10)
into fine values(3,'18-Jan-24',01)
into fine values(4,'19-Feb-24',02)
SELECT * FROM DUAL;
4 rows created.
7 rows selected.