码迷,mamicode.com
首页 > 数据库 > 详细

oracle plsql exception例外

时间:2017-02-19 15:57:14      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:错误   error   sel   blog   any   into   多行   cep   rac   

not_data_found例外:

--系统列外
set serveroutput on

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例外:

 1 --系统例外: too_many_rows
 2 
 3 set serveroutput on
 4 declare
 5 
 6   pename emp.ename%type;
 7 
 8 begin
 9 
10   select ename into pename from emp where deptno = 10;
11 
12 exception
13   when too_many_rows then dbms_output.put_line(select into 匹配多行);
14   when others  then dbms_output.put_line(其他);
15 end;
16 /

 

算数或转换例外:

 1 --系统例外 : value_error
 2 
 3 set serveroutput on
 4 
 5 declare
 6   
 7   pnum number;
 8 begin
 9   pnum := abc;
10   
11 exception
12   when value_error then dbms_output.put_line(算术或转换错误);
13   when others then dbms_output.put_line(其他);
14 end;
15 /

 

0不能做除数例外:

 1 --系统例外 zero_divide
 2 set serveroutput on
 3 
 4 declare
 5 
 6   pnum number;
 7 begin
 8 
 9   pnum := 1/0;
10   
11 exception
12   when zero_divide then dbms_output.put_line(0不能做除数);
13   when others then dbms_output.put_line(其他);
14 end;
15 /

 

自定义例外:

--自定义例外: 

set serveroutput on

declare

  cursor cemp is select ename from emp where deptno =50;
  pename emp.ename%type;

  --自定义列外
  not_emp_data exception;
  
begin
  open cemp;
    
  fetch cemp into pename;
  
  if cemp%notfound then
      raise not_emp_data;
  end if;
  --如果程序程序中出现例外,oracle会通过pmon(process monitor)自动关闭清理资源
  close cemp;  

exception 
  when not_emp_data then dbms_output.put_line(自定义例外:没有查询到数据);
  when others then dbms_output.put_line(其他列外);
end;
/

 

 

知识点出处:http://www.imooc.com/learn/360

oracle plsql exception例外

标签:错误   error   sel   blog   any   into   多行   cep   rac   

原文地址:http://www.cnblogs.com/xxyfhjl/p/6415775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!