动态列头(时间字段)

if object_id('tempdb..#tb') is not null
   drop table #tb
select fworkno,convert(varchar(100),date,112) date,classes_id into #tb from ck_scheduling where date<'2012-10-07'
declare @f varchar(max),
        @sql varchar(max)
set @f =  stuff(( select distinct ',[' + convert(varchar(10),date,112) + ']' from #tb for xml path('')),1,1,'')
set @sql='select * from #tb pivot (max(classes_id) for date in ('+@f+'))a'
print @sql
exec(@sql)

posted @ 2012-10-04 14:45  随风ˇ止步  阅读(187)  评论(0编辑  收藏  举报