3

我有一个这样的查询:

select empno,name
from emp
where job = 'CLERK'
and empno = :empno

如果我传递的 empno 为 null,我想显示所有符合 job = 'CLERK' 条件的记录。如果 empno 是一个特定的数字,那么它应该过滤工作和 empno。

无论如何在不使用 PLSQL 的情况下在 SQL 中执行此操作?

4

2 回答 2

6
and (empno = :empno or :empno is null)
于 2012-07-04T14:43:45.393 回答
3

如果传递参数为空,则类似这样的东西,而不是用实际的列值替换它......

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.
于 2012-07-04T14:44:06.337 回答