Reputation: 43
Query 1:
create or replace procedure toUp(code in number)
is sname staff_master.staff_name%type;
recnotfound exception;
begin
select staff_name into sname from staff_master where staff_code=code;
if sname is NULL then
raise recnotfound;
else
update staff_master set staff_name=upper(staff_name) where staff_code=code;
end if;
exception
when recnotfound then dbms_output.put_line('Record not found');
end;
Query 2:
declare
commsn emp.comm%type;
no_comm exception;
begin
select comm into commsn from emp where empno=7369;
if commsn is NULL then
raise no_comm;
else
dbms_output.put_line('Comm is '||commsn);
end if;
exception
when no_comm then dbms_output.put_line('Commsn for emp doesnt exist');
end;
Here in Query 1 I'm checking whether sname is null.. However, when I pass an invalid code as a parameter to the procedure.. sname should be NULL and hence the exception 'recnotfound' must get raised.. but it is showing the following error:
SQL> exec toUp(7369);
BEGIN toUp(7369); END;
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "LAB06TRG15.TOUP", line 6
ORA-06512: at line 1
But when I do the same with Query 2 it is working as expected.. I guess it has something to do with how varchar2 is checked for null.. Am I doing it correctly?
I modified the code as follows :
create or replace procedure toUp(code in number)
is
sname staff_master.staff_name%type;
recnotfound exception;
begin
select staff_name into sname from staff_master where staff_code=code;
if sname is NULL then
dbms_output.put_line('a');
raise recnotfound;
else
dbms_output.put_line('b');
--update staff_master set staff_name=upper(staff_name) where staff_code=code;
end if;
exception
when recnotfound then dbms_output.put_line('Record not found');
when no_data_found then raise recnotfound;
end;
I get :
BEGIN toUp(7369); END;
*
ERROR at line 1:
ORA-06510: PL/SQL: unhandled user-defined exception
ORA-06512: at "LAB06TRG15.TOUP", line 16
ORA-01403: no data found
ORA-06512: at line 1
How do I solve this?
P.S. I want to do this using Exception only.. Its part of an assignment ..
Upvotes: 3
Views: 6776
Reputation: 230
This is a sample procedure created regarding your query. Please modify accordingly and try it should work. Thanks
create or replace procedure av_stack_test(sr_no_var in number)
as
nme avrajit.name%type;
no_rec exception;
num_count number;
begin
select count(*) into num_count from avrajit
where sr_no=sr_no_var;
if num_count>0 then
select name into nme from avrajit
where sr_no=sr_no_var;
update avrajit
set name=nme
where sr_no=sr_no_var;
else
raise no_rec;
end if;
dbms_output.put_line(sr_no_var);
exception
when no_rec then
dbms_output.put_line('No rec found');
when others then
dbms_output.put_line('Some other exception');
end;
Upvotes: 0
Reputation: 41
Exception will be raised as soon as no record is returned by your SELECT query. The code will go into the exception there only and will not continue is that was your expectation.
Try this instead :
create or replace procedure toUp(code in number)
is sname staff_master.staff_name%type;err_count number;
recnotfound exception;
begin
select count(*) into err_count from staff_master where staff_code=code;
if count > 0 then
select staff_name into sname from staff_master where staff_code=code;
else
raise recnotfound;
Not sure whether the syntax is exactly correct, but I hope you get the drift
Upvotes: 0
Reputation: 52396
If a query returns no rows then an "ORA-01403: no data found" error is raised. Your expectation, I think, is that execution will continue but no value will have been assigned to the variable -- that's not the case.
If what you want to do is check for the existence of a record then use:
select count(*)
into row_found
from ...
where ...
and rownum = 1;
this is guaranteed to return a single row with a value of 0 or 1 into the row_found variable.
With regard to your edit, you are not handling the raising of the user defined exception in the exception handling block. Wrap the SELECT with a BEGIN-END-EXCEPTION.
begin
begin
select ..
exception when NO_DATA_FOUND then raise recnotfound;
end;
if sname is NULL then
dbms_output.put_line('a');
raise recnotfound;
end if;
exception
when recnotfound then dbms_output.put_line('Record not found');
end;
I'm not clear what you're trying to do here though. Is the sname ever going to be returned as null from the query?
Upvotes: 4
Reputation: 7017
Actually, exceptions happens even before your IF statement. If SELECT INTO statement doesn't return a row, ORA-01403 is thrown. You might expect that in this situation NULL value is assigned to variable, but it is not so and exception is thrown instead.
You must add exception handling in your stored procedure to get over it. Documentation on how to do that can be found here
Sorry, don't have ORACLE now, so I can't check it, but it should be something like this:
...
select staff_name into sname from staff_master where staff_code=code;
exception
when NO_DATA_FOUND then ...handle no data...;
when TOO_MANY_ROWS then ...handle too many data rows...;
...
Upvotes: 0