结果集插入临时表的问题
Create Table TEST
(TypeChar(1),
Statedatetime)
Insert TEST
Select 'A','2006-5-10 00:01:01' Union All
Select 'A','2006-5-1 00:05:01' Union All
Select 'A','2006-5-9 00:06:01' Union All
Select 'B','2006-5-1 00:03:01' Union All
Select 'B','2006-5-5 00:08:01' Union All
Select 'B','2006-5-1 00:09:01' Union All
Select 'C','2006-5-5 00:11:01' Union All
Select 'C','2006-5-9 00:02:01'
--select * from test
GO
--State不固定的情況下的
Declare @S Varchar(8000)
Select @S=''
Select @S=@S+',SUM(Case Convert(Varchar(10),state,120) When '''+state+ '''Then 1 Else 0 End) As ' + '[' + state + ']'
From (Select Distinct Convert(Varchar(10),state,120) As state From TEST ) A Order By state
Select @S='Select Type'+@S+' From TEST Group By Type'
EXEC(@S)
Drop Table TEST
GO
----------------------------------------
如上所示,如果我想把以上结果集插入到临时表#T中
改为 如:Select @S='Select Type'+@S+' into #T From TEST Group By Type'
这样的话运行EXEC(@S)的话 并没有插入到#T中 只提示影响的行数,
可是如果把@S print出来后 再把得到的sql语句单独运行 就可以
如何解决