首页 > 数据库 > Oracle > Oracle Procedure II
---if then els
if
end if,
单条件判断
---
|
1
2
3
4
5
6
7
8
9
10
11 |
declare
v_grade
char
(1);begin
v_grade :=
'B'
;
if v_grade =
'A'
then
dbms_output.put_line(
'哥真牛逼'
);
else
dbms_output.put_line(
'哥还得加油'
);
end
if;end
;/ |
---if then elsif then else end if, 多条件判断 ---
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
declare
v_grade
char
(1);begin
v_grade :=
'X'
;
if v_grade =
'A'
then
dbms_output.put_line(
'哥是优等生'
);
elsif v_grade =
'B'
then
dbms_output.put_line(
'哥成绩一般'
);
elsif v_grade =
'C'
then
dbms_output.put_line(
'哥是拖后腿'
);
else
dbms_output.put_line(
'哥不能用成绩衡量'
);
end
if;end
;/ |
---case when end case 判断 ---
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
declare
v_grade
char
(1);begin
v_grade :=
'A'
;
case
when
v_grade =
'A'
then
dbms_output.put_line(
'哥是优等生'
);
when
v_grade =
'B'
then
dbms_output.put_line(
'哥成绩一般'
);
when
v_grade =
'C'
then
dbms_output.put_line(
'哥是吊车尾'
);
else
dbms_output.put_line(
'哥不能用成绩衡量'
);
end
case
;end
; |
留了思考题,大家可以先尝试自己做,再看答案。
问题:请大家分别用if和case when的方法返回如下对应信息
工资在700-1200,返回:工资真tm低,要不要人活了!
工资在1200-1500,返回:工资好低
工资在1500-3000,返回:刚好能活下去
工资高于3000,返回:哥真牛逼
- - - - - - -
答案
1、---用if then elsif判断员工编号的生存情况---
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
declare
v_sal number;begin
select
sal
into
v_sal
from
emp
where
empno = 7839;
if v_sal >= 700
and
v_sal<1200
then
dbms_output.put_line(
'工资真tm低,要不要人活了!'
);
elsif v_sal >= 1200
and
v_sal<1500
then
dbms_output.put_line(
'工资好低'
);
elsif v_sal >= 1500
and
v_sal<3000
then
dbms_output.put_line(
'刚好能活下去'
);
elsif v_sal > 3000
then
dbms_output.put_line(
'哥很牛逼!'
);
end
if;end
;/ |
2、---用case when判断员工编号的生存情况---
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
declare
v_sal number;begin
select
sal
into
v_sal
from
emp
where
empno = 7839;
case
when
v_sal >= 700
and
v_sal < 1200
then
dbms_output.put_line(
'工资真tm低,没法活了'
);
when
v_sal >= 1200
and
v_sal < 1500
then
dbms_output.put_line(
'工资好低'
);
when
v_sal >= 1500
and
v_sal < 3000
then
dbms_output.put_line(
'刚好够活'
);
when
v_sal > 3000
then
dbms_output.put_line(
'哥真牛逼'
);
end
case
;end
;/ |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70013625/viewspace-3087076/ ,如需转载,请注明出处,否则将追究法律责任。