简体   繁体   English

在where子句中使用case

[英]Using case inside where clause

I´m trying to create a procedure that has a parameter called m_reaplicacao. 我正在尝试创建一个具有名为m_reaplicacao的参数的过程。 This parameter receives the values 'S' for Yes, 'N' for No and 'T' for all records. 此参数接收值“S”表示“是”,“N”表示“否”,“T”表示所有记录。 When the parameter is Yes, I should return the records with value equals to 9. When the parameter is No, I should return the records different of 9. And finally, when the the value is All, I should return all records from the table. 当参数为Yes时,我应该返回值等于9的记录。当参数为No时,我应该返回不同的9的记录。最后,当值为All时,我应该返回表中的所有记录。 With the code bellow, Oracle says: Compilation errors for PROCEDURE MYDB.CONTAS_A_PAGAR_SPS Error: PL/SQL: ORA-00905: missing keyword Line: 84 Text: ta.id_1a_cbr = 9; 使用下面的代码,Oracle说:PROCEDURE的编译错误MYDB.CONTAS_A_PAGAR_SPS错误:PL / SQL:ORA-00905:缺少关键字行:84文本:ta.id_1a_cbr = 9;

    select * from proposta ta
    where 
          ta.estado = 'RJ'
          and case
               when m_reaplicacao = 'S' then
                    ta.id_1a_cbr = 9;
               when m_reaplicacao = 'N' then
                    ta.id_1a_cbr <> 9
               else null
          end case; 

I saw a lot of posts, but I did not solve this one. 我看了很多帖子,但我没有解决这个问题。 Can someone help me, please? 有人能帮助我吗?

Don't use a CASE statement in a WHERE clause when you really want a simple combination of boolean evaluations. 当您真正想要一个布尔评估的简单组合时,不要在WHERE子句中使用CASE语句。

WHERE ta.estado = 'RJ'
  AND (    m_reaplicacao = 'T'
       OR (m_reaplicacao = 'S' AND ta.id_1a_cbr = 9)
       OR (m_reaplicacao = 'N' AND ta.id_1a_cbr <> 9)
      )

If for some reason you really do want to use a CASE statement, you'd need the CASE to return a value that you check in the WHERE clause. 如果由于某种原因您确实想要使用CASE语句,则需要CASE返回在WHERE子句中检查的值。 For example 例如

WHERE ta.estado = 'RJ'
  AND (CASE WHEN m_reaplicacao = 'S' AND ta.id_1a_cbr = 9 
            THEN 1
            WHEN m_reaplicacao = 'N' AND ta.id_1a_cbr <> 9 
            THEN 1
            WHEN m_reaplicacao = 'T'
            THEN 1
            ELSE 2
        END) = 1

This is not generally the clearest way to express this sort of condition, however. 然而,这通常不是表达这种情况的最明确的方式。

You cannot return expressions in CASE statements, easiest to add additional WHERE criteria sets: 您无法在CASE语句中返回表达式,最容易添加其他WHERE条件集:

select * 
from proposta ta
where ta.estado = 'RJ'
  and (
         (m_reaplicacao = 'S' AND ta.id_1a_cbr = 9)
      OR (m_reaplicacao = 'N' AND ta.id_1a_cbr <> 9)
       )

Not sure what you want to happen in the NULL situation. 不确定在NULL情况下你想要发生什么。

You can use CASE in select statements, like this: 您可以在select语句中使用CASE,如下所示:

with d as (
  select 'A' as val from dual
  union
  select 'B' as val from dual
  union
  select 'C' as val from dual
)
select *
from d
where 
    case 
        when val = 'A' then 1
        when val = 'B' then 1
        when val = 'Z' then 1
    end = 1; 

Here we're looking for rows where val in ('A','B','Z'). 在这里,我们正在寻找val('A','B','Z')中的行。 In this example, its another way of writing: 在这个例子中,它的另一种写作方式:

select ... where val in ('A','B','Z');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM