首页 > 代码库 > oracle sql语句中使用if逻辑

oracle sql语句中使用if逻辑

l在 SQL 语句中使用IF-THEN-ELSE 逻辑
l
l使用两种方法:
•CASE 表达式:SQL99的语法,类似Basic,比较繁琐
•DECODE 函数:Oracle自己的语法,类似Java,比较简介

 

 

 1 SQL> select ename,job, sal, case job when PRESIDENT then 1.1*sal 2   2                               when MANAGER then 1.2*sal 3   3                                when CLERK then 1.3*sal 4   4                                 else 1.4*sal 5   5                                end 涨后薪水 6   6  from emp 7   7  ; 8   9 ENAME      JOB             SAL       涨后薪水10 ---------- --------- --------- ----------11 SMITH      CLERK        800.00       104012 ALLEN      SALESMAN    1600.00       224013 WARD       SALESMAN    1250.00       175014 JONES      MANAGER     2975.00       357015 MARTIN     SALESMAN    1250.00       175016 BLAKE      MANAGER     2850.00       342017 CLARK      MANAGER     2450.00       294018 KING       PRESIDENT   5000.00       550019 TURNER     SALESMAN    1500.00       210020 JAMES      CLERK        950.00       123521 FORD       ANALYST     3000.00       420022 MILLER     CLERK       1400.00       182023 jack_1234              2000.00       280024  25 13 rows selected26 27 SQL> select ename,job, sal,decode(job,PRESIDENT,1.1*sal,28   2                                   MANAGER,1.2*sal,29   3                                   CLERK,1.3*sal,30   4                                           1.4*sal)涨后薪水31   5  from emp;32  33 ENAME      JOB             SAL       涨后薪水34 ---------- --------- --------- ----------35 SMITH      CLERK        800.00       104036 ALLEN      SALESMAN    1600.00       224037 WARD       SALESMAN    1250.00       175038 JONES      MANAGER     2975.00       357039 MARTIN     SALESMAN    1250.00       175040 BLAKE      MANAGER     2850.00       342041 CLARK      MANAGER     2450.00       294042 KING       PRESIDENT   5000.00       550043 TURNER     SALESMAN    1500.00       210044 JAMES      CLERK        950.00       123545 FORD       ANALYST     3000.00       420046 MILLER     CLERK       1400.00       182047 jack_1234              2000.00       280048  49 13 rows selected

 

oracle sql语句中使用if逻辑