Orale第13课.docVIP

  • 3
  • 0
  • 约1.08万字
  • 约 5页
  • 2017-06-13 发布于河南
  • 举报
Orale第13课

第13章 异常处理 一.异常基本知识 1.异常分类:预定义异常,自定义异常 二.预定义异常 1.no_data_found 未找到数据异常 2.too_many_rows 返回过多记录异常 3.others 其他异常 语法: exception when 异常1 then 处理语句; when 异常2 then 处理语句; when others then 必须最后一句 处理语句; 例: declare v_sname student.sname%type; begin select sname into v_sname from student where sno=250; dbms_output.put_line(v_sname); exception when no_data_found then dbms_output.put_line(数据没有找到!); when too_many_rows then dbms_output.put_line(数据不唯一,请使用游标!); when others then dbms_output.put_line(这个比较怪,没见过!); end; / 例: declare v_sname student.sname%type; begin select sname into v_sname from student; dbms_output.put_line(v_sname); exception when no_data_found then dbms_output.put_line(数据没有找到!); when too_many_rows then dbms_output.put_line(数据不唯一,请使用游标!); when others then dbms_output.put_line(这个比较怪,没见过!); end; / 题1:查看员工表中是否有superman这个人,如果有工资加100,如果没有插入一条记录(只需插入employee_id,last_name,salary为0,即可) declare v_salary employees.salary%type; begin select salary into v_salary from employees where last_name=superman; update employees set salary=v_salary+100 where last_name=superman; exception when no_data_found then insert into employees(employee_id,last_name,salary) values(250,superman,0); end; / 三.自定义异常 1.格式:声明?触发?引用 声明声明部分 声明 声明部分 执行部分 Raise 异常 触发 引用 异常处理部分 例:declare salary_level varchar(20); e_salary_level exception; 声明 begin salary_level:=d; if salary_level not in (a,b,c) then raise e_salary_level; 触发 end if; exception when e_salary_level then 引用 dbms_output.put_line(没有这个级别,没法给钱!); end; / 题:求学号为2000012的学生所有课程的平均分,如果平均分小于85,打印:没考好,失误了,否则打印:正常发挥(利用自定义异常来做) declare v_avg_grade sc.grade%type; e exception; begin select avg(grade) into v_avg_grade from sc where sno=2000012; if v_avg_grade85 then raise e; end if; dbms_output.put_line(很好); exception when e then dbms_output.put_line(这样不好!); end; /

文档评论(0)

1亿VIP精品文档

相关文档