Assignment4 PBL
Assignment4 PBL
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 status in stud table as “D”. Otherwise display
message “Term granted” and set the status in stud table as “ND”
SQL> declare
2 mroll number(10);
3 matt number(10);
4 begin
5 mroll:=&mroll;
6 select att into matt from stud where roll_no = mroll;
7 if matt < 75 then
8 dbms_output.put_line(mroll || 'is detained');
9 update stud set status = 'D' where roll_no = mroll;
10 else
11 dbms_output.put_line(mroll || 'is not detained');
12 update stud set status = 'ND' where roll_no = mroll;
13 end if;
14 exception
15 when no_data_found then
16 dbms_output.put_line(mroll || 'is not found');
17 End;
46 88 ND
59 93 ND
19 56 D
2 74 D
18 41 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.
SQL> declare
2 mwithdrwal int;
3 macc_no int;
4 mbalance int;
5 begin
6 macc_no:=&macc_no;
7 select balance into mbalance from account_master where acc_no=macc_no;
8 dbms_output.put_line('Enter the amount want to be withdraw');
9 mwithdrwal:=&mwithdrwal;
10 if mbalance > mwithdrwal then
11 dbms_output.put_line('Your witdrawl is successful');
12 update account_master set balance = balance - mwithdrwal where acc_no = macc_no;
13 else
14 dbms_output.put_line('Insufficient Balance');
15 end if;
16 exception
17 when no_data_found then
18 dbms_output.put_line('Incorrect Data');
19 end;
20 /
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
SQL> Declare
2 mclient_id number(10);
3 mbal number(10);
4 BRV EXCEPTION;
5 Begin
6 mclient_id:=&mclient_id;
7 select bal_due into mbal from client_master where client_id=mclient_id;
8 if mbal<0 then
9 raise BRV;
10 else
11 dbms_output.put_line(mclient_id || 'balance is:'||mbal);
12 end if;
13 Exception
14 When BRV then
15 dbms_output.put_line('Business Rule Violated.');
16 end;
17 /
SQL> /
Enter value for mclient_id: 002
old 6: mclient_id:=&mclient_id;
new 6: mclient_id:=002;
2balance is:15000
SQL> /
Enter value for mclient_id: 003
old 6: mclient_id:=&mclient_id;
new 6: mclient_id:=003;
3balance is:8000
SQL> /
Enter value for mclient_id: 004
old 6: mclient_id:=&mclient_id;
new 6: mclient_id:=004;
Business Rule Violated.
SQL> /
Enter value for mclient_id: 005
old 6: mclient_id:=&mclient_id;
new 6: mclient_id:=005;
SQL> DECLARE
2 mroll INT;
3 mnameOfBook VARCHAR(30);
4 mdoi DATE;
5 days INT;
6 mstatus CHAR;
7 fine INT;
8 BEGIN
9 mroll := &Roll_no;
10 mnameOfBook := '&Name_of_Book';
11 SELECT DateOfIssue INTO mdoi FROM Borrower WHERE Roll_no = mroll AND
NameOfBook = mnameOfBook;
12 SELECT Status INTO mstatus FROM Borrower WHERE Roll_no = mroll AND
NameOfBook = mnameOfBook;
13 IF mstatus = 'R' THEN
14 dbms_output.put_line('Book already returned.');
15 ELSE
16 days := sysdate - mdoi;
17 IF days <= 15 THEN
18 dbms_output.put_line('Book returned with no fine.');
19 UPDATE Borrower SET Status = 'R' WHERE Roll_no = mroll AND NameOfBook =
mnameOfBook;
20 ELSE
21 IF days <= 30 THEN
22 fine := (days-15) * 5;
23 ELSE
24 fine := 15 * 5 + (days - 30) * 50;
25 END IF;
26 dbms_output.put_line('Book returned with fine amount: ' || fine);
27 UPDATE Borrower SET Status = 'R' WHERE Roll_no = mroll AND NameOfBook =
mnameOfBook;
28 INSERT INTO Fine VALUES(mroll,sysdate, fine);
29 END IF;
30 END IF;
31 EXCEPTION
32 WHEN NO_DATA_FOUND THEN
33 dbms_output.put_line('Incorrect Information.');
34 END;
35 /
Enter value for roll_no: 46
old 9: mroll := &Roll_no;
new 9: mroll := 46;
Enter value for name_of_book: DSA
old 10: mnameOfBook := '&Name_of_Book';
new 10: mnameOfBook := 'DSA';
Book returned with fine amount: 55
SQL> /
Enter value for roll_no: 59
old 9: mroll := &Roll_no;
new 9: mroll := 59;
Enter value for name_of_book: DBMS
old 10: mnameOfBook := '&Name_of_Book';
new 10: mnameOfBook := 'DBMS';
Book returned with fine amount: 45
SQL> /
Enter value for roll_no: 57
old 9: mroll := &Roll_no;
new 9: mroll := 57;
Enter value for name_of_book: Opps
old 10: mnameOfBook := '&Name_of_Book';
new 10: mnameOfBook := 'Opps';
Book returned with fine amount: 18075
SQL> /
Enter value for roll_no: 18
old 9: mroll := &Roll_no;
new 9: mroll := 18;
Enter value for name_of_book: Wings of Fire
old 10: mnameOfBook := '&Name_of_Book';
new 10: mnameOfBook := 'Wings of Fire';
Book already returned.