Oracle Procedure II

来源:这里教程网 时间:2026-03-03 22:02:27 作者:

注:本文的数据都来源于,oracle自带的emp表。

---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 ;
/

相关推荐