L Use If-then-else logic in SQL statements LL uses two methods: CASE expression: SQL99 syntax, similar to basic, cumbersome decode functions: Oracle's own syntax, Java-like, comparative introduction
1Sql> SelectEname,job, Sal, CaseJob when 'President' Then 1.1*Sal2 2 when 'MANAGER' Then 1.2*Sal3 3 when 'Clerk' Then 1.3*Sal4 4 Else 1.4*Sal5 5 Endsalary after rise6 6 fromEMP7 7 ;8 9 ename JOB SAL salary after riseTen ---------- --------- --------- ---------- OneSMITH Clerk800.00 1040 AALLEN salesman1600.00 2240 -WARD salesman1250.00 1750 -JONES MANAGER2975.00 3570 theMARTIN salesman1250.00 1750 -BLAKE MANAGER2850.00 3420 -CLARK MANAGER2450.00 2940 -KING President5000.00 5500 +TURNER salesman1500.00 2100 -JAMES Clerk950.00 1235 +FORD ANALYST3000.00 4200 AMILLER Clerk1400.00 1820 atjack_12342000.00 2800 - - -rows Selected - -Sql> SelectEname,job, Sal,decode (Job,'President',1.1*Sal, - 2 'MANAGER',1.2*Sal, in 3 'Clerk',1.3*Sal, - 4 1.4*Sal) pay rise after to 5 fromEMP; + - ename JOB SAL salary after rise the ---------- --------- --------- ---------- *SMITH Clerk800.00 1040 $ALLEN salesman1600.00 2240Panax NotoginsengWARD salesman1250.00 1750 -JONES MANAGER2975.00 3570 theMARTIN salesman1250.00 1750 +BLAKE MANAGER2850.00 3420 ACLARK MANAGER2450.00 2940 theKING President5000.00 5500 +TURNER salesman1500.00 2100 -JAMES Clerk950.00 1235 $FORD ANALYST3000.00 4200 $MILLER Clerk1400.00 1820 -jack_12342000.00 2800 - the -Rows selected
Use if logic in Oracle SQL statements