我正在尝试运行以下查询:
select a.*,
case when NVL (SELECT max(b.field1)
FROM b
where b.field2 = a.tbl_a_PK , 'TRUE') = 'TRUE'
then 'has no data in b'
else 'has data in b' end as b_status
from a
我检查过并且 nvl 中的选择只返回 1 个值(所以那里应该没有问题)。
但是我收到“ORA-00936:缺少表达”
最佳答案
NVL()
需要 2 个参数:要测试的表达式和默认值,例如nvl(some_field, 111)
.您只需要通过大括号隔离查询参数并提供第二个参数,如以下语句:
select nvl( (select 1 from dual), 34) from dual
在您的变体解析器中,
SELECT
之后需要逗号关键字,无法解析剩余的字符串。您的语句必须如下所示:
select
a.*,
case when NVL(
( SELECT max(b.field1)
FROM b
where b.field2 = a.tbl_a_PK
),
'TRUE'
) = 'TRUE'
then 'has no data in b'
else 'has data in b' end as b_status
from a
希望这可以帮助 ...
更新
在性能方面最好使用
exists
而不是 max
:select
a.*,
case when exists
( SELECT null
FROM b
where b.field2 = a.tbl_a_PK
and
b.field2 is not null
and
rownum = 1
),
then 'has data in b'
else 'has no data in b' end as b_status
from a
关于oracle - NVL 中的 Select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16695978/