如果all / one参数为/?

时间:2012-09-06 11:42:06

标签: sql

create Procedure Proc_selectEmp
(
@name varchar(10) = null,
@lname varchar(10) = null,
@id varchar(10) = null

)
as
begin

select * from Emp
where 
(@name is null or name = @name) 
and (@lname is null or lname = @lname) 
and (@id is null or id = @id)
end

我的基本查询:select * from Emp。如果用户输入名称,则会添加,查询将为:

select *  
from Emp 
where name = txtname.Text

这是我的sp。它处理得当。

使用isnull()case还有其他选择吗?

1 个答案:

答案 0 :(得分:2)

这样的东西
where   ISNULL(@name,name) = name  
and     ISNULL(@lname,lname) = lname
and     ISNULL(@id,id) = id