zoukankan      html  css  js  c++  java
  • PL/SQL 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;

  • 相关阅读:
    Atitit 趋势管理之道 attilax著
    Atitit 循环处理的新特性 for...else...
    Atitit 2017年的技术趋势与未来的大技术趋势
    atitit 用什么样的维度看问题.docx 如何了解 看待xxx
    atitit prj mnrs 项目中的几种经理角色.docx
    Atitit IT办公场所以及度假村以及网点以及租房点建设之道 attilax总结
    Atitit 工具选型的因素与方法 attilax总结
    Atitit.团队文化建设影响组织的的一些原理 法则 定理 效应 p826.v4
    Atiitt 管理方面的误区总结 attilax总结
    Atitit 未来趋势把控的书籍 attilax总结 v3
  • 原文地址:https://www.cnblogs.com/sheying/p/8651476.html
Copyright © 2011-2022 走看看