我试图将CASE条件合并到where子句中,因为我们不能使用if条件。我有条件代码。我想将其包括在where子句中。为此,我正在使用CASE,但它似乎不起作用,并给出以下错误:
ORA-00905: missing keyword
我尝试了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)
如果满足以下条件,则代替以下内容:
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;
答案 0 :(得分:2)
“如果条件”可以从字面上翻译为:
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)
并且可以简化为:
(: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)
答案 1 :(得分:0)
情况不是这样。您不能返回条件,只能返回值
尝试或
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)
)