如果传递null值,则返回所有行

时间:2012-07-04 14:42:01

标签: sql oracle

我有这样的查询:

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

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

无论如何要在SQL中执行此操作而不使用PLSQL?

2 个答案:

答案 0 :(得分:5)

and (empno = :empno or :empno is null)

答案 1 :(得分:2)

如果pass参数为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.