Sunteți pe pagina 1din 4

AIM: Write PL/SQL procedure for an application using exception handling

SQL> set serveroutput on;

Q1). Write a Pl/SQL procedure for zero divide exception


SQL> ed zd_excep.sql;
declare
x number(5);
d number(5);
q number(5);
begin
x:=&divident;
d:=&divisor;
if((d!=0)) then
q:=x/d;
dbms_output.put_line('the quotient is '|| q);
else
raise zero_divide;
end if;
exception
when zero_divide then dbms_output.put_line('division with 0 is not
possible');
end;
/
SQL> @zd_excep.sql;
Enter value for divident: 22
old
6:
x:=&divident;
new
6:
x:=22;
Enter value for divisor: 4
old
7:
d:=&divisor;
new
7:
d:=4;
the quotient is 6
PL/SQL procedure successfully completed.
SQL> @zd_excep.sql;
Enter value for divident: 77
old
6:
x:=&divident;
new
6:
x:=77;
Enter value for divisor: 0
old
7:
d:=&divisor;
new
7:
d:=0;
division with 0 is not possible
PL/SQL procedure successfully completed.

Q2). Create a table student with attributes sid,sname,age


SQL> create table student(sid number(3),sname varchar2(15),age
number(3));

Table created.
SQL> insert into student values(22,'sudhakar',32);
1 row created.
SQL> insert into student values(77,'praneeth',28);
1 row created.
SQL> insert into student values(55,'rajesh',27);
1 row created.
SQL> select * from student;
SID
---------22
77
55

SNAME
AGE
--------------- ---------sudhakar
32
praneeth
28
rajesh
27

Q3). Write a Pl/SQL procedure for no data found exception


SQL> ed ndf_excep.sql;
declare
name student.sname%type;
id number(3);
begin
id:=&id;
select sname into name from student where sid=id;
if (name is not null) then
dbms_output.put_line('Student name is' ||name);
else
raise no_data_found;
end if;
exception
when no_data_found then dbms_output.put_line('no studnet with the roll
number ' ||id);
end;
/
SQL> @ndf_excep.sql;
Enter value for id: 22
old
5: id:=&id;
new
5: id:=22;
Student name issudhakar
PL/SQL procedure successfully completed.

SQL> @ndf_excep.sql;
Enter value for id: 88
old
5: id:=&id;
new
5: id:=88;
no studnet with the roll number 88
PL/SQL procedure successfully completed.

Q4). Write a Pl/SQL procedure for user defined exception


SQL> ed ud_excep.sql;
declare
name student.sname%type;
sage student.age%type;
my_excep exception;
id number(3);
begin
id:=&sid;
select sname into name from student where sid=id;
if(name='chp') then
select age into sage from student where sid=id;
dbms_output.put_line('student age is' || age);
else
raise my_excep;
end if;
exception
when my_excep then dbms_output.put_line('the id you entered is not
correct ');
when no_data_found then dbms_output.put_line('the id is not table');
end;
/
SQL> @ud_excep.sql;
Enter value for sudhakar_id: 22
old
7: id:=&sudhakar_id;
new
7: id:=22;
sudhakar age is32
PL/SQL procedure successfully completed.
SQL> @ud_excep.sql;
Enter value for sudhakar_id: 77
old
7: id:=&sudhakar_id;
new
7: id:=77;
the id you entered is not sudhakar id
PL/SQL procedure successfully completed.
SQL> @ud_excep.sql;
Enter value for sudhakar_id: 66
old
7: id:=&sudhakar_id;
new
7: id:=66;
the id is not table

PL/SQL procedure successfully completed.

Q5). Write a Pl/SQL procedure for case not found exception


SQL> ed snf_excep.sql;
declare
x number(3);
p real;
begin
x:=&one_or_two;
case x
when 1 then dbms_output.put_line('you are number one');
when 2 then dbms_output.put_line('you are second');
else raise case_not_found;
end case;
exception
when case_not_found then dbms_output.put_line('enter 1 or 2 only. try
again');
end;
/
SQL> @snf_excep.sql;
Enter value for one_or_two: 1
old
5: x:=&one_or_two;
new
5: x:=1;
you are number one
PL/SQL procedure successfully completed.
SQL> @snf_excep.sql;
Enter value for one_or_two: 2
old
5: x:=&one_or_two;
new
5: x:=2;
you are second
PL/SQL procedure successfully completed.
SQL> @snf_excep.sql;
Enter value for one_or_two: 22
old
5: x:=&one_or_two;
new
5: x:=22;
enter 1 or 2 only. try again
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> exit;

S-ar putea să vă placă și