ORACLE1.23 if case when

if (分3类)

  java

  if (条件) {

      ....

  }

 

  pl/sql

  if 条件 then

      .....

  end if;

----------------

select * from tt3

select age from tt3 where id=1

----------------------

declare

  myage tt3.age%type;

begin

  select age into myage from tt3 where id=1;

  --if (age>=19) {

  --   System.out.println("成年人");

  --}

  if myage>=19 then

      dbms_output.put_line('成年人');

  end if;

end;

---------------------------

if else

  java

  if(条件){

    ...

  }else{

    ...

  }

 

  pl/sql

  if 条件 then

 

  else

   

  end if;

 

declare

  myage tt3.age%type;

begin

  select age into myage from tt3 where id=1;

  if myage>=18 then

      dbms_output.put_line('成年人');

  else

    dbms_output.put_line('未成年人');   

  end if;

end;

 

update tt3 set age=19 where id=1

commit;

-------------------------

if else if else

  java

  if(条件1){

     ....

  }else if(条件2) {

    ....

  }else {

 

  }

 

  pl/sql

  if 条件1 then

   

  elsif 条件2 then

 

  else

   

  end if;

------------------

declare

  myage tt3.age%type;

begin

  select age into myage from tt3 where id=1;

  if myage>=50 then

      dbms_output.put_line('中老年人');

  elsif myage>=18 then

      dbms_output.put_line('成年的年青人');

  else

    dbms_output.put_line('未成年人');   

  end if;

end;

 

-----------------

case (也分3种)

 

第一种:有selector,并且执行语句

 

  case [selector]

    when 1 then 语句1;

    when 2 then 语句2;

    when 3 then 语句3;

    else 语句4

  end case;

 

declare

  my_user tt3%rowtype;

begin

  select * into my_user from tt3 where id=1;

  -- my_user.city

  case my_user.city

    when '北京' then dbms_output.put_line('长城很好玩');

    when '上海' then dbms_output.put_line('浦东很好玩');

    when '珠海' then dbms_output.put_line('南方IT最好玩');

    else

      dbms_output.put_line('不如到珠海南方玩一下');

  end case;

end;

 

update tt3 set city='上海' where id=1

commit;

 

 

第二种:有selector,单不执行语句,而是返回一个值

既然有返回值,那么就可以赋值给其它的变量

 

  case [selector]

    when 1 then '返回结果1';

    when 2 then '返回结果2';

    when 3 then '返回结果3';

    else '返回结果4'

  end case;

 

---------------

select * from tt3 where id=1;

--------------

declare

  my_user tt3%rowtype;

  show_message varchar2(200);

begin

  select * into my_user from tt3 where id=1;

  -- my_user.city

  show_message:=

  case my_user.city

    when '北京' then '长城'

    when '上海' then '浦东'

    when '珠海' then '南方'

    else '珠海南方'

  end;

  dbms_output.put_line(my_user.user_name||'('||my_user.city||')'||show_message||'很好玩');

end;

----------

第三种:没有selector,也不执行语句,

但是可以每个when单独的表达式,然后最后返回值

 

 

declare

  my_user tt3%rowtype;

  show_message varchar2(200);

begin

  select * into my_user from tt3 where id=1;

  show_message:=

  case

    when my_user.age>50 then '来自于'||my_user.city|| my_user.user_name ||'是一个中老年年人'

    when my_user.age>=18 then my_user.user_name || '是一个成年人'

    else

        my_user.user_name || '是个未成年人,'||'可以到'||my_user.city||'找她'

  end;

  dbms_output.put_line(show_message);

end;

 

update tt3 set age=12 where id=1

commit;

原文地址:https://www.cnblogs.com/wyj1212/p/8651263.html