当我想在比较中使用它们时,我应该如何处理PL/SQL存储过程中的空参数?

Pip*_*ner 6 sql oracle plsql

我有一个带有参数的存储过程name,我希望在where子句中使用该参数来匹配列的值,例如

where col1 = name
Run Code Online (Sandbox Code Playgroud)

当然null,null由于方式null有效,因此无法匹配.我需要做什么吗

where ((name is null and col1 is null) or col1 = name)
Run Code Online (Sandbox Code Playgroud)

在这样的情况下,或者有更简洁的方式吗?

and*_*ndr 7

您可以decode按以下方式使用功能:

where decode(col1, name, 0) is not null
Run Code Online (Sandbox Code Playgroud)

引用SQL参考:

在DECODE函数中,Oracle认为两个空值是等效的.

  • @Adam Hawkes在你的例子中你已经指定了`decode`的第4个参数 - 如果没有找到匹配则默认值.如果未指定第4个参数,`decode`将返回`null`,因此如果我们关注作者的问题,我们的代码示例是相同的:比较`null`值. (2认同)

Jok*_*ilä 6

我认为你自己的建议是最好的方法.


Ton*_*ews 5

你所做的是正确的.有一种更简洁的方式,但它并不是真的更好:

where nvl(col1,'xx') = nvl(name,'xx')
Run Code Online (Sandbox Code Playgroud)

麻烦的是,你必须确保用于nulls的值('xx'是我的例子)实际上不能成为数据中的实际值.