编写一个匿名PL/SQL程序块,调用局部(本地)函数(输入参数是部门号)查询并显示10号部门的人数。要求函数体内有对不存在部门号的异常处理,给用户的错误指示信息是:不存在部门号为**的部门!
Declare
Function dept_count(dept_no number)
return number
Is
v_deptcount number(2);
count_null exception;
Begin
select count(*) into v_deptcount from emp where deptno=dept_no;
if v_deptcount=0 then
raise count_null;
else
return v_deptcount;
end if;
EXCEPTION
WHEN count_null THEN
raise_application_error (-20001, '不存在部门号为' ¦ ¦ TO_CHAR(dept_no) ¦ ¦ '的部门!');
End; Begin
DBMS_OUTPUT.PUT_LINE('60号部门的人数为:' ¦ ¦ TO_CHAR(dept_count(60)));
End; Declare
*
ERROR 位于第 1 行:
ORA-20001: 不存在部门号为60的部门!
ORA-06512: 在line 16
ORA-06512: 在line 20 由于表中无60号部门,故会发生异常。这个PL/SQL块除了我自定义的那个异常外,难道还会有其他异常错误吗。假如把异常放在执行块里处理,就不会有问题。但是我现在想把它放在声明块里处理,如何让他只提示‘不存在部门号为**的部门!’ 请大家帮帮忙 ,谢谢!
EMP表见附件
该帖包含附件:http://d.download.csdn.net/down/488664/showvin
Declare
Function dept_count(dept_no number)
return number
Is
v_deptcount number(2);
count_null exception;
Begin
select count(*) into v_deptcount from emp where deptno=dept_no;
if v_deptcount=0 then
raise count_null;
else
return v_deptcount;
end if;
EXCEPTION
WHEN count_null THEN
raise_application_error (-20001, '不存在部门号为' ¦ ¦ TO_CHAR(dept_no) ¦ ¦ '的部门!');
End; Begin
DBMS_OUTPUT.PUT_LINE('60号部门的人数为:' ¦ ¦ TO_CHAR(dept_count(60)));
End; Declare
*
ERROR 位于第 1 行:
ORA-20001: 不存在部门号为60的部门!
ORA-06512: 在line 16
ORA-06512: 在line 20 由于表中无60号部门,故会发生异常。这个PL/SQL块除了我自定义的那个异常外,难道还会有其他异常错误吗。假如把异常放在执行块里处理,就不会有问题。但是我现在想把它放在声明块里处理,如何让他只提示‘不存在部门号为**的部门!’ 请大家帮帮忙 ,谢谢!
EMP表见附件
该帖包含附件:http://d.download.csdn.net/down/488664/showvin
2 v_deptcount number(2);
3 count_null exception;
4 Begin
5 select count(*) into v_deptcount from tablename where col='11111';
6 if v_deptcount=0 then
7 raise count_null;
8 end if;
9 EXCEPTION
10 WHEN count_null THEN
11 raise_application_error (-20001, '不存在部门号为 11111的部门!');
12 End;
13 /
Declare
v_deptcount number(2);
count_null exception;
Begin
select count(*) into v_deptcount from ac02 where aab001='11111';
if v_deptcount=0 then
raise count_null;
end if;
EXCEPTION
WHEN count_null THEN
raise_application_error (-20001, '不存在部门号为 11111的部门!');
End;
ORA-20001: 不存在部门号为 11111的部门!
ORA-06512: 在line 12