declare @s varchar(255)
set @s='订购号=1008 and 维修站号=1004'
exec('select * from yourtable where '+@s)在前台把输入的条件生成一个条件字符串,后台调用此字符串就可以了!

解决方案 »

  1.   

    select * from tablename
    where 订购号=1 or 维修站号=132 or 维修站名称='asdf' or 生成日期='2003-08-26'...
      

  2.   

    declare @参数1 varchar(100),@参数2 varchar(100)
    select * from 表 where 维修站号=isnull(@参数1,维修站号) and 维修站名称=isnull(@参数2,维修站名称)这样你没有给值的话就会忽略
      

  3.   

    select * from tablename
    where 订购号=1 and 维修站号=132 and 维修站名称='asdf' and 生成日期='2003-08-26'...
      

  4.   

    主要是前台生成查询条件,没什么SQL技巧。
      

  5.   

    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
      

  6.   

    把对应的值传进来: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语句中应用
      

  7.   

    认同这个:主要是前台生成查询条件,没什么SQL技巧。
      

  8.   

    用存储过程来实现:--查询的存储过程
    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','','','',''