【问题标题】:Why does the SQL 'select 1 from dual where 0=null;' pass in the oracle为什么 SQL 'select 1 from dual where 0=null;'通过神谕
【发布时间】:2016-11-23 10:49:05
【问题描述】:

我在 ORACLE 中输入以下 SQL 并通过:

select 1 from dual where 0=null; 

为什么SQL通过了,没有编译错误看起来很奇怪。而且,如果null是表中的varchar2列,也会通过。

create table test
(
  a varchar2(100),
  b varchar2(100)
)

insert into test(a,b) VALUES (null, 'abc');
commit;

select * from test where a=0

更新:

因为a列是varchar2类型,0是数字类型,如果a不包含null,会报错。

create table test
(
  a varchar2(100),
  b varchar2(100)
)

insert into test(a,b) VALUES ('abc', 'abc');
commit;


select * from test where a=0

--ORA-01722 "invalid number"

谢谢。

【问题讨论】:

  • 您对该查询有何期望?
  • “通过”是什么意思? 0 = null 没有语法错误。它永远不会评估为真。
  • 您的问题到底是什么?这里有什么意外?
  • 这与where 'foo' = 'bar'有何不同?
  • @Bohemian:看来 OP 对编译错误、运行时错误和成功编译并执行但不返回任何内容的语句之间的区别感到困惑。请重新打开。

标签: sql database oracle


【解决方案1】:

它通过了,因为syntax 是正确的。它包含所有必需元素,因此可以对其进行解析。然后它被执行。请查看语句如何processed
它不返回任何结果,因为 where 条件不成立。

【讨论】:

    【解决方案2】:

    这是因为NULL 是一个值,就像任何其他值一样。如果不使用is,(不)等式检查将失败,但语法仍然有效。

    我们假设您传入一个变量 :1,而不是直接编写 NULL,然后像这样重写您的查询:

    select 1 from dual where 0 = :1
    

    在这种情况下,:1 的值可能是任何值,这是由调用代码确定的,而您编写的 SELECT 语句不知道调用代码将传入什么值。

    因此,Oracle 不可能说此语句无效,即使它可以完全按照您所写的那样表示:

    select 1 from dual where 0 = null
    

    因为它也可能是:

    select 1 from dual where 0 = 0
    

    【讨论】:

      猜你喜欢
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      相关资源
      最近更新 更多