源地址:http://zhidao.baidu.com/question/340437545.html?seed=0无意当中看到这个问题,自己没什么好的方法,故求大神。

解决方案 »

  1.   

    我只想知道sql是怎么写 在程序里实现谁都会
      

  2.   

    declare @str varcahr(100)='select id'
    if(select col1 from tb where id=@id) is not null
     set @str=@str+',col1'
    if(select col2 from tb where id=@id) is not null
     set @str=@str+',col3'
    ...
    exec(@str+' where id='''+@id+'')
      

  3.   

    declare @str varchar(100)='select id',
    @id int=1;
    if(select col1 from ccc where id=@id) is not null
     set @str=@str+',col1'
    if(select col2 from ccc where id=@id) is not null
     set @str=@str+',col2'
    if(select col3 from ccc where id=@id) is not null
     set @str=@str+',col3'
     --print @str
    exec(@str+' from ccc where id='''+@id+'''')