编写一个匿名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
解决方案 »
- oracle 彻底删除表空间和用户,急问
- 动态参数:表名,字段名,filter条件的存储过程
- 关于Oracle数据库备份的问题
- oracle11g+win7:ORA-12154: TNS:could not resolve the connect identifier specified
- oracle redo 写条件引起的问题?
- 字符串联怎么实现,请大师进来谢谢
- rownum和distinct一并使用
- (500)求1000万条记录的高效分页,各种方法均可,在jsp环境下?(表如下)给个例子!!!!(急等)
- 如何只选取一条记录?
- oracle(+)的优化,我是新人这个问题需要老手帮忙解决,老手们求帮助!!!
- “w我们”字符串在oracle中存放占用多少空间?
- 删除出错
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