日期:2014-05-18  浏览次数:20521 次

SQL查询数据为空的列不显示


源地址:http://zhidao.baidu.com/question/340437545.html?seed=0

无意当中看到这个问题,自己没什么好的方法,故求大神。




------解决方案--------------------
以前讨论过这个 用动态拼接。
------解决方案--------------------
SQL code
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+'')