[英]Use of CASE inside where clause
I am trying to incorporate the CASE condition inside the where clause as we cant use if condition. 我试图将CASE条件合并到where子句中,因为我们不能使用if条件。 I have this if condition code with me. 我有条件代码。 I want to include it in where clause. 我想将其包括在where子句中。 For this purpose i am using the CASE but it doesn't seem to be working, giving the following error: 为此,我正在使用CASE,但它似乎无法正常工作,并给出以下错误:
ORA-00905: missing keyword
I have tried CASE condition like: 我已经尝试过CASE条件,例如:
AND (CASE
WHEN p_trx_date_low Is Null and p_trx_date_high Is Null
Then a.trx_date = a.trx_date
WHEN p_trx_date_low Is Not Null and p_trx_date_high Is Null
Then a.trx_date >= p_trx_date_low
WHEN p_trx_date_low Is Null and p_trx_date_high Is Not Null
Then a.trx_date <= p_trx_date_high
WHEN p_trx_date_low Is Not Null and p_trx_date_high Is Not Null
Then a.trx_date between p_trx_date_low and p_trx_date_high
End CASE)
in place of below if condition: 代替以下if条件:
If :p_trx_date_low Is Null and :p_trx_date_high Is Null Then
:p_trx_date_clause := ' and a.trx_date = a.trx_date ';
ElsIf :p_trx_date_low Is Not Null and :p_trx_date_high Is Null Then
:p_trx_date_clause := ' and a.trx_date >= :p_trx_date_low ';
ElsIf :p_trx_date_low Is Null and :p_trx_date_high Is Not Null Then
:p_trx_date_clause := ' and a.trx_date <= :p_trx_date_high ';
ElsIf :p_trx_date_low Is Not Null and :p_trx_date_high Is Not Null Then
:p_trx_date_clause := ' and a.trx_date between :p_trx_date_low and :p_trx_date_high ';
End If;
The "if condition" could literally be translated to this: “如果条件”可以从字面上翻译为:
AND (:p_trx_date_low IS NULL AND :p_trx_date_high IS NULL AND a.trx_date = a.trx_date)
OR (:p_trx_date_low IS NOT NULL AND :p_trx_date_high IS NULL AND a.trx_date >= :p_trx_date_low)
OR (:p_trx_date_low IS NULL AND :p_trx_date_high IS NOT NULL AND a.trx_date <= :p_trx_date_high)
OR (:p_trx_date_low IS NOT NULL AND :p_trx_date_high IS NOT NULL AND a.trx_date BETWEEN :p_trx_date_low AND :p_trx_date_high)
And could be simplified to: 并且可以简化为:
(:p_trx_date_low IS NULL OR a.trx_date >= :p_trx_date_low) AND
(:p_trx_date_high IS NULL OR a.trx_date <= :p_trx_date_high)
That's not how case works. 情况不是这样的。 You can't return a condition, only a value 您不能返回条件,只能返回值
Try OR 尝试或
AND (
(p_trx_date_low Is Null and p_trx_date_high Is Null and a.trx_date = a.trx_date)
OR
(p_trx_date_low Is Not Null and p_trx_date_high Is Null and a.trx_date >= p_trx_date_low)
OR
(p_trx_date_low Is Null and p_trx_date_high Is Not Null and a.trx_date <= p_trx_date_high)
OR
(p_trx_date_low Is Not Null and p_trx_date_high Is Not Null and a.trx_date between p_trx_date_low and p_trx_date_high)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.