避免老年癡呆,趕快記下來。

declare @colname varchar(200)

 SELECT  @colname = STUFF(( SELECT 

                                '],[' + t2.vcolumndef

                        FROM EC.tb_trclass_subjectitemdef   AS t2

                        where vtype = '3' and nsubjectno = 1

                        ORDER BY '],[' +  t2.vcolumndef

                        FOR XML PATH('')

                      ), 1, 2, '') + ']'

 print @colname                       

 declare @a varchar(1000)

 set @a = 'SELECT * from

            (select * from EC.tb_trclass_subjectitem_a  where nsubjectno = 1  and vtype =''3'') src

           PIVOT (count(vlink) FOR vcolumndef IN ('+@colname+')) AS pvt'             

 

    EXEC (@a)

創作者介紹
創作者 flywang 的頭像
flywang

長不大的老小孩

flywang 發表在 痞客邦 留言(0) 人氣()