- 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;
/
您可能关注的文档
最近下载
- 部编版语文小学六年级下册第一单元集体备课(教材解读).pptx VIP
- 电子商务进农村培训PPT最新版PPT课件.pptx VIP
- 起重机双折线卷筒的相关参数研究与确定.pdf VIP
- 风对起飞、着陆的影响及修正.ppt VIP
- 2025-2030中国AG玻璃行业供需形势与竞争格局分析研究报告.docx
- 元好问摸鱼儿.ppt VIP
- 湖南师大附中2025届高三语文月考试卷“不可谏”和“想当初”写作指导及范文.docx VIP
- (高清版)DB42∕T 1770-2021 《建筑节能门窗工程技术标准》.pdf VIP
- 3-6岁幼儿家庭亲子阅读文章.doc VIP
- 2026年县直事业单位招聘职业能力测试题库精选.docx VIP
原创力文档

文档评论(0)