How should I deal with null parameters in a PL/SQL stored procedure when I want to use them in comparisons?(当我想在比较中使用它们时,我应该如何处理 PL/SQL 存储过程中的空参数?)
问题描述
我有一个带有参数 name
的存储过程,我想在 where
子句中使用它来匹配列的值,例如
I have a stored procedure with a parameter name
which I want to use in a where
clause to match the value of a column i.e. something like
where col1 = name
当然,由于 null
的工作方式,这当然无法将 null
匹配到 null
.我需要做吗
Now of course this fails to match null
to null
because of the way null
works. Do I need to do
where ((name is null and col1 is null) or col1 = name)
在这种情况下,或者有更简洁的方法吗?
in situations like this or is there a more concise way of doing it?
推荐答案
您可以使用 decode
功能如下:
You can use decode
function in the following fashion:
where decode(col1, name, 0) is not null
从 SQL 参考中引用:
Cite from SQL reference:
在一个 DECODE 函数中,Oracle 考虑两个 null 是等价的.
In a DECODE function, Oracle considers two nulls to be equivalent.
这篇关于当我想在比较中使用它们时,我应该如何处理 PL/SQL 存储过程中的空参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我想在比较中使用它们时,我应该如何处理 PL/SQL 存储过程中的空参数?


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01