oracle16 例外

时间:2023-03-09 06:44:44
oracle16  例外
例外处理 

 例外的分类
oracle将例外分为预定义例外,非预定义例外和自定义例外三种。
预定义例外用于处理常见的oracle错误
非预定义例外用于处理预定义例外不能处理的例外
自定义例外用于处理与oracle错误无关的其它情况 例外传递
如果不处理例外我们看看会出现什么情况:
案例,编写一个过程,可接收雇员的编号,并显示该雇员的姓名。
问题是,如果输入的雇员编号不存在,怎样去处理呢?
Sql代码
.--例外案例   
.declare    
.--定义   
.v_ename emp.ename%type;   
.begin  
.--   
.select ename into v_ename from emp where empno=&gno;   
.dbms_output.put_line('名字:'||v_ename)     
./  
执行,弹出框,看图:

oracle16  例外

随便输个不存在的编号,回车,会抛出异常,显示:
ORA-: 未找到数据
ORA-: 在line
Sql代码
.declare    
.--定义   
.v_ename emp.ename%type;   
.begin  
.--   
.select ename into v_ename from emp where empno=&gno;   
.dbms_output.put_line('名字:'||v_ename)     
.exception    
.   when no_data_found then  (no_data_found是预定义例外)
.   dbms_output.put_line('编号没有!');   
./  
执行,输入一个不存在的编号,回车,显示:
编号没有!
处理预定义例外
预定义例外是由pl/sql所提供的系统例外。当pl/sql应用程序违反了oracle 规定的限制时,则会隐含的触发一个内部例外。pl/sql为开发人员提供了二十多个预定义例外。我们给大家介绍常用的例外。 预定义例外 case_not_found
在开发pl/sql块中编写case语句时,如果在when子句中没有包含必须的条件分支,就会触发case_not_found的例外:
Sql代码
.create or replace procedure sp_pro6(spno number) is  
.  v_sal emp.sal%type;   
.begin  
.  select sal into v_sal from emp where empno = spno;   
.  case  
.    when v_sal <  then  
.      update emp set sal = sal +  where empno = spno;   
.    when v_sal <  then  
.      update emp set sal = sal +  where empno = spno;   
.  end case;   
.exception   
.  when case_not_found then  
.    dbms_output.put_line('case语句没有与' || v_sal || '相匹配的条件');   
.end;   预定义例外 cursor_already_open
当重新打开已经打开的游标时,会隐含的触发例外cursor_already_open
Sql代码
.declare  
.  cursor emp_cursor is select ename, sal from emp;   
.begin  
.  open emp_cursor;   
.  for emp_record1 in emp_cursor loop   
.    dbms_output.put_line(emp_record1.ename);   
.  end loop;   
.exception   
.  when cursor_already_open then  
.    dbms_output.put_line('游标已经打开');   
.end;   
./   预定义例外 dup_val_on_index
在唯一索引所对应的列上插入重复的值时,会隐含的触发例外dup_val_on_index例外
Sql代码
.begin  
.  insert into dept values (, '公关部', '北京');   
.exception   
.  when dup_val_on_index then  
.    dbms_output.put_line('在deptno列上不能出现重复值');   
.end;   预定义例外 invalid_cursor
当试图在不合法的游标上执行操作时,会触发该例外
例如:试图从没有打开的游标提取数据,或是关闭没有打开的游标。则会触发该例外
Sql代码
.declare  
.  cursor emp_cursor is select ename, sal from emp;   
.  emp_record emp_cursor%rowtype;   
.begin  
.  --open emp_cursor; --打开游标   
.  fetch emp_cursor into emp_record;   
.  dbms_output.put_line(emp_record.ename);   
.  close emp_cursor;   
.exception   
.  when invalid_cursor then  
.    dbms_output.put_line('请检测游标是否打开');   
.end;   预定义例外 invalid_number
当输入的数据有误时,会触发该例外
比如:数字100写成了loo就会触发该例外
Sql代码
.begin  
.  update emp set sal= sal + 'loo';   
.exception   
.  when invalid_number then  
.    dbms_output.put_line('输入的数字不正确');   
.end;   预定义例外 no_data_found
  下面是一个pl/sql块,当执行select into 没有返回行,就会触发该例外
Sql代码
.declare  
.    v_sal emp.sal%type;   
.begin    
.    select sal into v_sal from emp   
.    when ename='&name';   
.exception   
.    when no_data_found then  
.    dbms_output.put_line('不存在该员工');   
.end;  
 预定义例外 too_many_rows
当执行select into语句时,如果返回超过了一行,则会触发该例外。
Sql代码
.declare  
.  v_ename emp.ename%type;   
.begin  
.  select ename into v_ename from emp;   
.exception   
.  when too_many_rows then  
.    dbms_output.put_line('返回了多行');   
.end;   预定义例外 zero_divide
当执行2/0语句时,则会触发该例外。 预定义例外 value_error
当在执行赋值操作时,如果变量的长度不足以容纳实际数据,则会触发该例外value_error,比如:
Sql代码
.declare  
.  v_ename varchar2();   
.begin  
.  select ename into v_ename from emp where empno = &no1;   
.  dbms_output.put_line(v_ename);   
.exception   
.  when value_error then  
.    dbms_output.put_line('变量尺寸不足');   
end; 
其它预定义例外(这些例外不是在pl/sql里触发的,而是在用oracle时触发的,所以取名叫其它预定义例外)
.login_denied
当用户非法登录时,会触发该例外
.not_logged_on
如果用户没有登录就执行dml操作,就会触发该例外
.storage_error
如果超过了内存空间或是内存被损坏,就触发该例外
.timeout_on_resource
如果oracle在等待资源时,出现了超时就触发该例外 非预定义例外
  非预定义例外用于处理与预定义例外无关的oracle错误。使用预定义例外只能处理21个oracle错误,而当使用pl/sql开发应用程序时,可能会遇到其它的一些oracle错误。比如在pl/sql块中执行dml语句时,违反了约束规定等等。在这样的情况下,也可以处理oracle的各种例外,因为非预定义例外用的不多,这里我就不举例了。
 处理自定义例外
  预定义例外和自定义例外都是与oracle错误相关的,并且出现的oracle错误会隐含的触发相应的例外;而自定义例外与oracle错误没有任何关联,它是由开发人员为特定情况所定义的例外.
问题:请编写一个pl/sql块,接收一个雇员的编号,并给该雇员工资增加1000元,如果该雇员不存在,请提示。
Sql代码
.--自定义例外   
.create or replace procedure ex_test(spNo number)   
.is  
.begin  
.--更新用户sal   
.update emp set sal=sal+ where empno=spNo;   
.end;   
./   运行,该过程被成功创建。
SQL> exec ex_test();
PL/SQL过程被成功完成 这里,编号为56是不存在的,刚才的报异常了,为什么现在不报异常呢?
因为刚才的是select语句 现在是update语句
怎么解决这个问题呢? 修改代码,如下:
Sql代码
.--自定义例外   
.create or replace procedure ex_test(spNo number)   
.is  
.--定义一个例外   
.myex exception;   
.begin  
.--更新用户sal   
.update emp set sal=sal+ where empno=spNo;   
.--sql%notfound这是表示没有更新成功   
.--raise myex;触发myex   
.if sql%notfound then  
.raise myex;   
.end if;   
.exception   
.when myex then  
.dbms_output.put_line('没有更新任何用户');   
.end;   
./   现在再测试一次:
SQL> exec ex_test();
没有更新任何用户