日期:2014-05-17 浏览次数:20567 次
--t1换成你的表名
declare @sql nvarchar(max)=N'declare @s nvarchar(max)='''';';
select @sql=@sql+ 'if exists(select * from t1 where '+name+' is not null) set @s=@s+'''+name+','''
from sys.columns where object_id=object_id('t1')
select @sql=@sql+'set @s=substring(@s,1,len(@s)-1);';
select @sql=@sql+'set @s=''select '' +@s +'' from t1'' ;';
select @sql=@sql+'exec(@s)';
exec (@sql)