我是靠谱客的博主 舒适狗,这篇文章主要介绍ORACEL条件判断语句,现在分享给大家,希望可以做个参考。

一、IF-ELSE判断

1、if...then...end if;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
案例1:输出年龄大于19岁的学生信息 declare type cur is ref cursor;--声明ref游标类型 type rec is record( --声明记录数据类型 r1 number, r2 varchar2(20), r3 float, r4 varchar2(20) ); cur_01 cur; --声明ref游标变量 rec_01 rec; --声明记录类型变量 begin open cur_01 for --打开游标 select id,name,age,major from student; loop fetch cur_01 into rec_01; --获取游标所在行数据并存储在rec_01中 exit when cur_01%notfound; --当游标在最后一行时退出 if rec_01.r3 > 19 then --当年龄大于19时输出     dbms_output.put('学号:'||rec_01.r1||' ');     dbms_output.put('姓名:'||rec_01.r2||' ');     dbms_output.put('年龄:'||rec_01.r3||' ');     dbms_output.put_line('专业:'||rec_01.r4||' '); end if; end loop; close cur_01; --关闭游标 end;

2、if...then...else...end if;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
案例2:输出及格人数和不及格人数 declare type cur is ref cursor;--声明ref游标类型 cur_01 cur;--声明ref游标变量 type rec is record( --声明记录数据类型 m1 VARCHAR2(32), m2 VARCHAR2(32), m3 float ); rec_01 rec; --声明记录类型变量 r2 float :=0; r3 float :=0; begin open cur_01 for select c_name,s_name,score from s_course; loop fetch cur_01 into rec_01; --获取游标所在行数据并存储到记录变量中 exit when cur_01%notfound; if rec_01.m3 >= 60 then     r2:=r2+1; else     r3 :=r3+1; end if; end loop; close cur_01; dbms_output.put_line('及格人数:'||r2); dbms_output.put_line('不及格人数:'||r3); end;

3、if...then...elsif...else...end if;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
案例3:输出学生成绩等级(不及格,及格,优秀) declare type cur is ref cursor; cur_01 cur; type rec is record( m1 VARCHAR2(32), m2 VARCHAR2(32), m3 float ); rec_01 rec; r1 varchar2(20); begin open cur_01 for select c_name,s_name,score from s_course; loop fetch cur_01 into rec_01; exit when cur_01%notfound; if rec_01.m3 < 60 then r1 := '不及格'; elsif rec_01.m3 >= 60 and rec_01.m3 < 80 then r1 := '及格'; else r1 := '优秀'; end if; dbms_output.put('课程名称:'||rec_01.m1||' '); dbms_output.put('学生姓名:'||rec_01.m2||' '); dbms_output.put_line('成绩等级:'||r1); end loop; close cur_01; end;

二、CASE-WHEN判断

1、case...when...then...else...end case;

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
案例4:输出学生成绩等级(不及格,及格,优秀) declare type cur is ref cursor;--声明ref游标类型 cur_01 cur;--声明ref游标变量 type rec is record( --声明记录数据类型 m1 VARCHAR2(32), m2 VARCHAR2(32), m3 float ); rec_01 rec; --声明记录类型变量 r1 varchar2(20); --声明变量 begin open cur_01 for --打开游标 select c_name,s_name,score from s_course; loop fetch cur_01 into rec_01; --获取游标所在行数据并存储到记录变量中 exit when cur_01%notfound; case when rec_01.m3 < 60 then r1 :='不及格'; when rec_01.m3 >= 60 and rec_01.m3 < 80 then r1 :='及格'; else r1 :='优秀'; end case; dbms_output.put('课程名称:'||rec_01.m1); dbms_output.put('学生姓名:'||rec_01.m2); dbms_output.put_line('成绩等级:'||r1); end loop; close cur_01; end;

三、DECODE()函数

1、decode(value,if1...then1...if2...then2...else)

复制代码
1
2
案例5:查询男生女生数量 select sum(decode(sex,'男',1,0)) as 男生,sum(decode(sex,'女',1,0)) as 女生 from student;

最后

以上就是舒适狗最近收集整理的关于ORACEL条件判断语句的全部内容,更多相关ORACEL条件判断语句内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部