oracle - NVL 中的 Select 语句

标签 oracle nvl

我正在尝试运行以下查询:

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/

相关文章:

java - 使用 myBatis 映射嵌套的 Oracle CURSOR (ResultSet)

regex - 如何在oracle(pl/sql)中转义字符串中的特殊字符

Oracle nvl2 在存储过程 PLS-00201 : identifier 'NVL2' must be declared 中不起作用

javascript - js功能优化

java - 无法使用 19.3 JDBC 驱动程序连接到 Oracle 19.3

oracle - 什么会影响 Oracle 对字符串表达式数据类型的确定?

database - Oracle 查询优化器的诊断输出

mysql - 左连接和连接列显示空白而不是 (null)

java - Oracle nvl 需要将 null 插入数字字段

database - 为什么 NVL2 函数的第二个和第三个参数不接受 LONG 值?