如何在oracle中使用CASE查询

时间:2015-04-08 17:36:05

标签: oracle stored-procedures

您好我正在尝试运行此查询,但在where语句中,Case正在发出错误。我要做的是,如果i_id is >= 284cnint <=65之后附加另一个and,则将Select cnint as Id, cnabbv as Code, rtrim(cnname) as Name, i_id as CId, cntry as CCode from money.country where cntype = 1 and (CASE WHEN i_id = 284 THEN cnint <= 65 ELSE cnint > 65 END) order by cnname; 附加到查询中。此查询位于存储过程中。

{{1}}

此查询位于存储过程中。

2 个答案:

答案 0 :(得分:3)

那里不支持。试试这个:

Select cnint as Id, cnabbv as Code, rtrim(cnname) as Name, i_id as CId, cntry as CCode 
from money.country where cntype = 1 and
((i_id = 284 AND cnint <= 65) OR (coalesce(i_id, 0) != 284 and cnint > 65))
order by cnname;

答案 1 :(得分:2)

你可以用case这样的

来完成
select cnint as Id, 
       cnabbv as Code, 
       rtrim(cnname) as Name, 
       i_id as CId, 
       cntry as CCode 
from money.country 
where cntype = 1 and ( ( i_id = 284 AND cnint <= 65 ) OR (coalesce(i_id, 0) != 284 and cnint>65) )
order by cnname;

case返回标量值时,您可以使用case这样做 但是case

更具可读性
   select cnint as Id, 
           cnabbv as Code, 
           rtrim(cnname) as Name, 
           i_id as CId, 
           cntry as CCode 
    from money.country 
    where cntype = 1  AND 1 = (CASE WHEN (i_id = 284 AND cnint <= 65 ) OR (coalesce(i_id, 0) != 284 and cnint >65)  
                                    THEN 1
                                    ELSE 0 
                               END)
order by cnname;