declare @sql varchar(8000)
set @sql='select * from [table] '+case when @style is not null then 'where style='+@style else '' end+case when @keys is not null then ' and keys like '+@keys else '' end
exec(@sql)

解决方案 »

  1.   

    select * from table where (@style is null or style=@style) and (@keys is null or keys like @keys)
      

  2.   

    改正一下
    select @style=isnull(@style,''),@keys=isnull(@keys,'')
    declare @sql varchar(8000)
    set @sql='select * from [table] '+case when @style<>'' then 'where style='+@style else '' end+case when @keys<>'' and @style<>'' then ' and keys like '+@keys when @keys<>'' and @style='' then 'where keys like '+@keys else '' end
    exec(@sql)
      

  3.   

    gahade(我们了解历史时,我们已成为历史)
    declare @sql varchar(8000)
    set @sql='select * from [table] '+case when @style is not null then 'where style='+@style else '' end+case when @keys is not null then ' and keys like '+@keys else '' end
    exec(@sql)
    ---------------------------------------------------
    不管style和keys有不有时都出错
      

  4.   

    什么错误?
    把你的代码和错误贴出来看看.
    有没有table这个表啊?
      

  5.   

    当style=csqj时,出现以下错误
    列名 'csqj' 无效。
      

  6.   

    declare @sql varchar(8000)
    declare @style char(2)
    select @style='b'
    declare @keys char(2)
    select @keys='b'
    set @sql='select * from table '+
    case 
    when @style is null and @keys is null
    then ''
    else 
    'where ' 
    end
    +
    case
    when @style is not null
    then 'style='''+@style+''' and '
    end 
    +
    case
    when @keys is not null
    then 'keys='''+@keys+''''
    end
    print @sql
    我刚试过这个,借用了下,呵呵~~试试吧
      

  7.   

    declare @sql varchar(8000)
    declare @style char(2)
    select @style='b'
    declare @keys char(2)
    select @keys='b'
    set @sql='select * from table '+
    case 
    when @style is null and @keys is null
    then ''
    else 
    'where ' 
    end
    +
    case
    when @style is not null
    then 'style='''+@style+''' and '
    end 
    +
    case
    when @keys is not null
    then 'keys='''+@keys+''''
    endprint @sql
      

  8.   

    如果是字符型的就要加引号,如果是数值型就要转为字符型再用加号连接
    例:
    declare @style varchar(20)
    set @style='aaa'
    declare @keys int
    set @keys=100
    select @style=isnull(@style,''),@keys=isnull(@keys,0)
    declare @sql varchar(8000)
    set @sql='select * from [table] '+case when @style<>'' then 'where style='''+@style+'''' else '' end+case when @keys<>0 and @style<>'' then ' and keys = '+rtrim(@keys) when @keys<>0 and @style='' then 'where keys = '+rtrim(@keys) else '' end
    exec(@sql)
      

  9.   

    好像可以了。多谢了。
    请问一下,为什么要加那么多引号,还有当为like时引号要怎么写?
      

  10.   

    is null运算用在变量上的时候会有运算的问题啊?
    难怪老兄用了isnull()函数,长见识了!
      

  11.   

    我弄错了,is null运算也可以用。前面那个出错的地方是因为When...then少了else块;如果没有else块就导致表达式返回NULL值,当然得不到结果了。今天收获不小,谢谢了!
    declare @sql varchar(100)
    declare @style char(1)
    set @style='b'
    declare @keys char(1)
    set @keys='b'
    set @sql='select * from table '+
    case 
    when @style is null and @keys is null
    then ''
    else 
    'where ' 
    end
    +
    case
    when @style is not null
    then 'style='''+rtrim(@style)+''' and '
    else ''
    end 
    +
    case
    when @keys is not null
    then 'keys like '''+rtrim(@keys)+''''
    else ''
    end
    print @sql
      

  12.   

    上面的字符串连接有错,这是俺的最终完美版,呵~~不要给删了啊~
    declare @sql varchar(100)
    declare @style char(1)
    declare @keys char(1)
    set @style='a'
    set @keys='b'
    set @sql='select * from table '+
    case 
    when @style is not null
    then 
    'where style='''+rtrim(@style)+'''' 
    else ''
    end
    +
    case
    when @style is not null and @keys is not null
    then
    ' and keys like '''+rtrim(@keys)+''''
    else ''
    end
    +
    case
    when @style is null and @keys is not null
    then
    'keys like '''+rtrim(@keys)+''''
    else ''
    end
    print @sql
      

  13.   

    roklba(摇滚de梦) 
       
    好像可以了。多谢了。
    请问一下,为什么要加那么多引号,还有当为like时引号要怎么写?
    ----------------------------------------------------declare @style varchar(20)
    set @style='aaa'
    declare @keys varchar(20)
    set @keys='xyz'
    select @style=isnull(@style,''),@keys=isnull(@keys,'')
    declare @sql varchar(8000)
    set @sql='select * from [table] '+case when @style<>'' then 'where style='''+@style+'''' else '' end+case when @keys<>'' and @style<>'' then ' and keys like '''+@keys+'''' when @keys<>'' and @style='' then 'where keys like '''+@keys+'''' else '' end
    exec(@sql)