2013-02-22 56 views
7

我想知道如果有如果可能达到像如果-ELSEIF-ELSE在Oracle SQL '条件'

一些事情 'IF-ELSEIF-ELSE' 的条件,我知道有一个“案例时,则 - '但'它一次只检查一个条件(如果我理解正确)。如何能够做到在Oracle SQL IF-ELSEIF-ELSE场景

+1

嵌套CASE语句是一种选择。我对甲骨文不太了解,不知道是否有更好的方法。 – 2013-02-22 10:49:35

+0

你是什么意思由_it检查一次只有一个条件_作为案例陈述应该有所帮助..如果不然后使用解码,但它只做平等检查 – asifsid88 2013-02-22 10:53:12

+0

请参阅http://stackoverflow.com/questions/14386881/using-if- else-in-oracle – glh 2013-02-23 07:38:38

回答

17

可以的if/else使用情况之类的语句T选用他的。

SELECT ename, CASE WHEN sal = 1000 THEN 'Minimum wage' 
        WHEN sal > 1000 THEN 'Over paid' 
        ELSE 'Under paid' 
       END AS "Salary Status" 
FROM emp; 
1

我知道有一个“的情况下,当-THEN-ELSE”,但它同时

你所描述的是一个简单的例子只检查一个 条件。 Oracle有两个案例类型:简单搜索(在这里看到更多信息http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm

SIMPLE

case A 
    when 1 then 'foo' 
    when 2 then 'bar' 
    else .. 
end 

搜寻过

case 
    when A=1 and B='A' then 'foo' 
    when D + C =1 and B !='A' then 'Bar' 
    else .. 
end 

你可能想使用搜索情况。你可以在PL/SQL或SQL中使用它们。例如,在SQL中

select .. 
    from table 
where case 
     when A=1 and B='A' then 'foo' 
     when D + C =1 and B !='A' then 'Bar' 
     else .. 
     end = 'foo' 
+0

我曾经使用过这样的东西,但它不起作用 – user964147 2013-02-22 11:02:34

+0

是的,我需要搜索的案例 – user964147 2013-02-22 11:02:53