You are on page 1of 5

Assignment No.

: 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”.

create table Stud(Roll number ,


Att number ,
Status varchar(10));

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;

Enter value for mroll: 2


old 5: mroll:=&mroll;
new 5: mroll:=2;

PL/SQL procedure successfully completed.


Enter value for mroll: 3
old 5: mroll:=&mroll;
new 5: mroll:=3;

PL/SQL procedure successfully completed.


Enter value for mroll: 4
old 5: mroll:=&mroll;
new 5: mroll:=4;
PL/SQL procedure successfully completed.
select * from Stud;

ROLL ATT STATUS


---------- ---------- ----------
1 70 D
2 90 ND
3 40 D
4 68 D

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.

create table acc(


acc_no number,
balance number,
status varchar(50));

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

When No_sufficient_bal then


Dbms_output.put_line('Sufficint balance is not available in
account');

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;

PL/SQL procedure successfully completed.

Enter value for acc_no: 200


old 10: macc:=&acc_no;
new 10: macc:=200;
Enter value for trans_amt: 300
old 11: trans_amt:=&trans_amt;
new 11: trans_amt:=300;

PL/SQL procedure successfully completed.

Enter value for acc_no: 300


old 10: macc:=&acc_no;
new 10: macc:=300;
Enter value for trans_amt: 123
old 11: trans_amt:=&trans_amt;
new 11: trans_amt:=123;

PL/SQL procedure successfully completed.

Enter value for acc_no: 400


old 10: macc:=&acc_no;
new 10: macc:=400;
Enter value for trans_amt: 10000
old 11: trans_amt:=&trans_amt;
new 11: trans_amt:=10000;

PL/SQL procedure successfully completed.

SQL> select * from acc;

ACC_NO BALANCE STATUS


---------- ---------- --------------------------------------------------
100 800 successful
200 9200 successful
300 4676 successful
400 7000 not success

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;

ROLL_NO DATEOFISS NAMEOFBOOK STATUS


---------- --------- ---------- -----
1 01-DEC-23 ABC
2 08-DEC-23 ABC
3 10-JAN-24 ABC
4 10-FEB-24 ABC

create table Fine(


roll_no number,
datef date,
amt number);
Table created.

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);

if datediff < 15 then


dbms_output.put_line('No Fine');
update borrowerx set status='NF' where roll_no=mroll and nameofbook=mbook;

elsif datediff > 15 and datediff < 30 then


mfine:=(datediff*5);
dbms_output.put_line('Fine is: '||mfine);
update borrowerx set status='F' where roll_no=mroll and nameofbook=mbook;
insert into fine values(mroll,sysdate,mfine);

elsif datediff > 30 then


mfine:=((datediff-30)*50+(30*5));
dbms_output.put_line('Fine is: '||mfine);
update borrowerx set status='F' where roll_no=mroll and nameofbook=mbook;
insert into fine values(mroll,sysdate,mfine);
end if;
end;
/
Enter value for mroll: 1
old 9: mroll:=&mroll;
new 9: mroll:=1;
Enter value for mbook: ABC
old 10: mbook:='&mbook';
new 10: mbook:='ABC';
PL/SQL procedure successfully completed.

Enter value for mroll: 2


old 9: mroll:=&mroll;
new 9: mroll:=2;
Enter value for mbook: ABC
old 10: mbook:='&mbook';
new 10: mbook:='ABC';
PL/SQL procedure successfully completed.

Enter value for mroll: 3


old 9: mroll:=&mroll;
new 9: mroll:=3;
Enter value for mbook: ABC
old 10: mbook:='&mbook';
new 10: mbook:='ABC';
PL/SQL procedure successfully completed.

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.

SELECT * FROM fine;

ROLL_NO DATEF AMT


---------- --------- ----------
1 16-FEB-24 2500
2 16-FEB-24 2150
3 16-FEB-24 500
1 10-JAN-24 100
2 22-DEC-23 10
3 18-JAN-24 1
4 19-FEB-24 2

7 rows selected.

You might also like