您的位置:首页 > 数据库 > Oracle

oracle数据库中的例外处理

2018-01-20 09:33 120 查看
-- 例外
--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 sql