declare @sql varchar(8000) set @sql='' select @sql=@sql+','+SEQName from M_XT_XLB group by SEQName set @sql=stuff(@sql,1,1,'')--去掉首个',' set @sql='select * from M_XT_XLB pivot(max([CurrentValue]) for SEQName in ('+@sql+')) a' exec(@sql)
或者
declare @sql varchar(8000) select @sql=isnull(@sql+',','')+SEQName from M_XT_XLB group by SEQName set @sql='select * from M_XT_XLB pivot(max(CurrentValue) for SEQName in ('+@sql+'))a' exec(@sql)