CREATE TABLE [gs] (
[V_Label] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[T_gs] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
------------------------------------------------------------------------------------------------------------------
declare @s varchar(8000)
set @s = ''
select @s = @s+',['+V_Var+']=sum(case V_Var when '''+ V_Var +''' then V_value end)' from base
set @s = stuff(@s,1,1,'')
exec('select '+ @s + ' into ##t from base')
set @s = ''
select @s = @s + ' union all select gs='''+rtrim(V_Label)+''',bds='''+ rtrim(T_gs) +''',jg='+T_gs+' from ##t' from gs
set @s = stuff(@s,1,10,'')
print @s
exec(@s)
Drop Table ##t