【发布时间】:2019-04-29 08:14:00
【问题描述】:
我正在尝试将 CASE 条件合并到 where 子句中,因为我们不能使用 if 条件。我有这个 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条件:
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;
【问题讨论】:
-
在 If 条件中您将比较条件作为字符串传递。
-
一般来说,在
WHERE子句(或ON 子句)中最好使用AND/OR结构而不是case。
标签: sql oracle case where-clause