select * from tablename where 订购号=1 or 维修站号=132 or 维修站名称='asdf' or 生成日期='2003-08-26'...
declare @参数1 varchar(100),@参数2 varchar(100) select * from 表 where 维修站号=isnull(@参数1,维修站号) and 维修站名称=isnull(@参数2,维修站名称)这样你没有给值的话就会忽略
select * from tablename where 订购号=1 and 维修站号=132 and 维修站名称='asdf' and 生成日期='2003-08-26'...
主要是前台生成查询条件,没什么SQL技巧。
declare @订购号 varchar(100),@维修站号 varchar(100),@维修站名称 varchar(100),@生成日期 datetimeselect * from tableName where 订购号 like case when isnull(@订购号,0)='0' then '%' else 订购号 end, and 维修站号 like case when isnull(@维修站号,0)='0' then '%' else 维修站号 end, and 维修站名称 like case when isnull(@维修站名称,0)='0' then '%' else 维修站名称 end, and 生成日期 like case when isnull(@生成日期,0)='0' then '%' else 生成日期 end
把对应的值传进来:declare @订购号 varchar(100),@维修站号 varchar(100),@维修站名称 varchar(100),@生成日期 datetime select * from tableName where 订购号 like case when isnull(@订购号,0)='0' then '%' else 订购号 end, and 维修站号 like case when isnull(@维修站号,0)='0' then '%' else 维修站号 end, and 维修站名称 like case when isnull(@维修站名称,0)='0' then '%' else 维修站名称 end, and 生成日期 like case when isnull(@生成日期,0)='0' then '%' else 生成日期 end如果在前台,可以把对应的sql变量转换为前台语言的变量,直接在语言的sql语句中应用
认同这个:主要是前台生成查询条件,没什么SQL技巧。
用存储过程来实现:--查询的存储过程 create procedure test @订购号 varchar(100) ,@维修站号 varchar(100) ,@维修站名称 varchar(100) ,@生成日期 datetime ,@是否结算 bit as declare @tj varchar(8000) select @tj='',@tj=@tj +case isnull(@订购号,'') when '' then '' else ' and [订购号]='''+@订购号+'''' end +case isnull(@维修站号,'') when '' then '' else ' and [维修站号]='''+@维修站号+'''' end +case isnull(@维修站名称,'') when '' then '' else ' and [维修站名称]='''+@维修站名称+'''' end +case isnull(@生成日期,'') when '' then '' else ' and [生成日期]='''+@生成日期+'''' end +' and [是否结算]='+cast(isnull(@是否结算,0) as varchar) if @tj<>'' set @tj=' where '+right(@tj,len(@tj)-5)exec('select * from 表 '+@tj go --调用 exec test '1','','','',''
where 订购号=1 or 维修站号=132 or 维修站名称='asdf' or 生成日期='2003-08-26'...
select * from 表 where 维修站号=isnull(@参数1,维修站号) and 维修站名称=isnull(@参数2,维修站名称)这样你没有给值的话就会忽略
where 订购号=1 and 维修站号=132 and 维修站名称='asdf' and 生成日期='2003-08-26'...
订购号 like case when isnull(@订购号,0)='0' then '%' else 订购号 end,
and 维修站号 like case when isnull(@维修站号,0)='0' then '%' else 维修站号 end,
and 维修站名称 like case when isnull(@维修站名称,0)='0' then '%' else 维修站名称 end,
and 生成日期 like case when isnull(@生成日期,0)='0' then '%' else 生成日期 end
select * from tableName where
订购号 like case when isnull(@订购号,0)='0' then '%' else 订购号 end,
and 维修站号 like case when isnull(@维修站号,0)='0' then '%' else 维修站号 end,
and 维修站名称 like case when isnull(@维修站名称,0)='0' then '%' else 维修站名称 end,
and 生成日期 like case when isnull(@生成日期,0)='0' then '%' else 生成日期 end如果在前台,可以把对应的sql变量转换为前台语言的变量,直接在语言的sql语句中应用
create procedure test
@订购号 varchar(100)
,@维修站号 varchar(100)
,@维修站名称 varchar(100)
,@生成日期 datetime
,@是否结算 bit
as
declare @tj varchar(8000)
select @tj='',@tj=@tj
+case isnull(@订购号,'') when '' then ''
else ' and [订购号]='''+@订购号+'''' end
+case isnull(@维修站号,'') when '' then ''
else ' and [维修站号]='''+@维修站号+'''' end
+case isnull(@维修站名称,'') when '' then ''
else ' and [维修站名称]='''+@维修站名称+'''' end
+case isnull(@生成日期,'') when '' then ''
else ' and [生成日期]='''+@生成日期+'''' end
+' and [是否结算]='+cast(isnull(@是否结算,0) as varchar)
if @tj<>''
set @tj=' where '+right(@tj,len(@tj)-5)exec('select * from 表 '+@tj
go
--调用
exec test '1','','','',''