我有一个这样的查询:
select empno,name
from emp
where job = 'CLERK'
and empno = :empno
如果我传递的 empno 为 null,我想显示所有符合 job = 'CLERK' 条件的记录。如果 empno 是一个特定的数字,那么它应该过滤工作和 empno。
无论如何在不使用 PLSQL 的情况下在 SQL 中执行此操作?
and (empno = :empno or :empno is null)
如果传递参数为空,则类似这样的东西,而不是用实际的列值替换它......
select empno,name from emp where
job = 'CLERK'
and empno = NVL(:empno ,empno)
NVL 的工作原理
NVL 函数的语法是:
NVL( string1, replace_with )
string1 is the string to test for a null value.
replace_with is the value returned if string1 is null.