-- 例外
--no_data_found
declare
pename emp.ename%type;
begin
select ename into pename from emp where empno=1234;
exception
when no_data_found then DBMS_OUTPUT.PUT_LINE('没有找到员工信息');
when others then dbms_output.put_line('其他意外');
end;
--too_many_rows
declare
pename emp.ename%type;
begin
select ename into pename from emp where deptno=10;
exception
when too_many_rows then DBMS_OUTPUT.PUT_LINE('匹配了多行');
when others then dbms_output.put_line('其他例外');
end;
--zero_divide
declare
pnum number;
begin
pnum:=1/0;
exception
when zero_divide then dbms_output.put_line('0不能做除数');
when others then dbms_output.put_line('其他例外');
end;
--value_error
declare
pnum number;
begin
pnum:='asd';
exception
when value_error then dbms_output.put_line('算数或转换错误');
when others then dbms_output.put_line('其他例外');
end;
--自定义例外
--查询50号部门员工
declare
cursor cemp is select ename from emp where deptno=50;
pename emp.ename%type;
no_emp_found exception;
begin
open cemp;
fetch cemp into pename;
if cemp%notfound then
rsiae no_emp_found;
end if;
close cemp;
exception
when no_emp_found then dbms_output.put_line('没有找到');
when others then dbms_output.put_line('其他');
end;
oracle数据库中的例外处理
最新推荐文章于 2022-10-24 23:14:28 发布