select * from 表 where 货物名称 like case when @hwmc = '' then '%' else @hwmc end
if @hwmc<>'' select * from 表 where 货物名称=@hwmc
不过,一般是先作判断:if @hwmc<>'' select * from 表 where 货物名称=@hwmc else select * from 表
select * from 表 where 货物名称 like (CASE WHEN @hwmc<>'' THEN @hwmc ELSE '%' END)
看漏了,是 select * from 表 where 货物名称 like case when @hwmc = '' then '%' else @hwmc end 这样,或者用if语句
declare @sql varchar(8000) declare @hwmc varchar(200) select @sql='select * from 表 ' if @hwmc ='' begin select @sql='select * from 表 ' end if @hwmc<>'' then begin select @sql=@sql+' where 货物名称='''+@hwmc+'''' end exec(@sql)
select * from 表 where 货物名称 = isnull(@hwmc,'')
没那么复杂吧,呵呵~~ select * from 表 where 货物名称 = isnull(@hwmc,'')
错了,sorry! select * from 表 where (货物名称 = isnull(@hwmc,'') or isnull(@hwmc,'') = '')
from 表
where @hwmc = '' or 货物名称 = @hwmc
select * from 表 where 货物名称=@hwmc
select * from 表 where 货物名称=@hwmc
else
select * from 表
select * from 表 where 货物名称 like case when @hwmc = '' then '%' else @hwmc end
这样,或者用if语句
declare @hwmc varchar(200)
select @sql='select * from 表 '
if @hwmc =''
begin
select @sql='select * from 表 '
end
if @hwmc<>'' then
begin
select @sql=@sql+' where 货物名称='''+@hwmc+''''
end
exec(@sql)
select * from 表 where 货物名称 = isnull(@hwmc,'')
select * from 表 where (货物名称 = isnull(@hwmc,'') or isnull(@hwmc,'') = '')