【例外传递】 如果不处理例外我们看看会出现什么情况: 案例,编写一个过程,可接收雇员的编号,并显示该雇员的姓名。 问题是,如果输入的雇员编号不存在,怎样去处理呢?
--例外案例 declare --定义 v_ename emp.ename%type; begin --执行 select ename into v_ename from emp where empno=&gno; dbms_output.put_line('名字:'||v_ename); --no_data_found是预定义的例外,它认为这些例外很常见 when no_data_found then dbms_output.put_line('编号没有!'); end; /
【处理预定义例外】 预定义例外是由pl/sql所提供的系统例外。当pl/sql应用程序违反了oracle规定的 限制时,则会隐含的触发一个内部例外。pl/sql为开发人员提供了20多个预定义例外。
【预定义例外 case_not_found】 在开发pl/sql块中编写case语句时,如果在wehn子句中没有包含必须的条件分支,就会 触发case_not_found的例外: 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<1000 then update emp set sal=sal+100 where empno=spno; when v_sal<2000 then update emp set sal=sal+200 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 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; /
【预定义例外 invalid_cursor】 当试图在不合法的游标上执行操作时,会触发该例外 例如:试图从没有打开的游标提取数据,或是关闭没有打开的游标。则会触发该例外 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; /
【预定义例外 invlide_number】 当输入的数据有误时,会触发该例外 比如:数字100写成了1oo就会触发该例外 begin update emp set sal=sal+'1oo'; exception when invalid_number then dbms_output.put_line('输入的数字不正确'); end; /
【预定义例外 no_data_found】 下面是一个pl/sql块,当执行select into 没有返回行,就会触发该例外 declare v_sal emp.sal%type; begin select sal into v_sal from emp where ename='&name'; exception when no_data_found then dbms_output.put_line('不存在该员工'); end;
【预定义例外 too_many_rows】 当执行select into 语句时,如果返回超过了一行,则会触发该例外。 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, 比如: declare v_ename varchar2(5); 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;
【其他预定义例外】 ①login_denide 当用户非法登录时,会触发该例外 ②not_logged_on 如果用户没有登录就执行dml操作,就会触发该例外 ③storage_error 如果超出了内存空间或是内存被损坏,就触发该例外 ④timeout_on_resource 如果oracle在等待资源时,出现了超时就触发该例外
【非预定义例外】 非预定义例外用于处理与预定义例外无关的oracle错误。使用预定义例外 只能处理21个oracle错误,而当使用pl/sql开发应用程序时,可能会遇到 其他一些oracle错误。比如在pl/sql块中执行dml语句时,违反了约束规定等 等,在这样的情况下,也可以处理oracle的各种例外,因为非预定义例外用 的不多,这里就不举例了
【处理自定义例外】【oracle不认为是错误的,用户可以自己将其定义为错误的】 预定义例外和自定义例外都是与oracle错误相关的,并且出现的oracle错误 会隐含的触发相应的例外;而自定义例外与oracle错误没有任何关联,他是由 开发人员为特定情况所定义的例外。 ?请编写一个pl/sql块,接收一个雇员的编号,并给该雇员工资增加1000元, 如果该雇员不存在,请提示。 create or replace procedure ex_test(spNo number) is begin --更新用户sal update emp set sal=sal+1000 where empno=spNo; end; /
@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 改进版 @@@@@@@@@@@@@@@@@@@@@@@@@@@@
create or replace procedure ex_test(spNo number) is --定义一个例外 myex exception; begin --更新用户sal update emp set sal=sal+1000 where empno=spNo; --sql%notfound这是表示没有update --raise myex; 触发myex例外 if sql%notfound then raise myex; end if; exception when myex then dbms_output.put_line('没有更新任何用户'); end; /
【视图】 视图时一个虚拟表,其内容有查询定义。同真实的表一样,视图包含一系列带有 名称的列和行数据。但是,视图并不在数据库中以存储的数据值集形式存在。 行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成。
【视图与表的区别】 ①表需要占用磁盘空间,视图不需要 ②视图不能添加索引 ③使用视图可以简化复杂查询 比如:学生选课系统 ④使用视图利于提高安全性 比如:不同用户查看不同视图