日期:2014-05-18  浏览次数:20561 次

我想将下面语句转换的结果写入到一张表里面怎么做?
declare @sql varchar(8000),@sql1 varchar(8000)
select @sql = isnull(@sql + ',' , '') + fdepartname1 from mm group by fdepartname1
select @sql1 = isnull(@sql1 + ',' , '') + fdepartname2 from mm group by fdepartname2
exec ('
select * from (select * from mm) a pivot (max(fbysl) for fdepartname1 in (' + @sql + ') ) b
pivot (max(fbyje) for fdepartname2 in (' + @sql1 + ') ) c  
') 

我想将这个执行的结果写入到一张表或视图里面该怎么做?请高手支招。。。。

------解决方案--------------------
SQL code

if object_id('tempdb..#test') is not null drop table #test;
declare @sql varchar(8000),@sql1 varchar(8000)
select @sql = isnull(@sql + ',' , '') + fdepartname1 from mm group by fdepartname1
select @sql1 = isnull(@sql1 + ',' , '') + fdepartname2 from mm group by fdepartname2
exec ('
select * into #test
 from (select * from mm) a pivot (max(fbysl) for fdepartname1 in (' + @sql + ') ) b
pivot (max(fbyje) for fdepartname2 in (' + @sql1 + ') ) c   
')

------解决方案--------------------
SQL code
insert into tb select * from (select * from mm) a pivot (max(fbysl) for fdepartname1 in (' + @sql + ') ) b
pivot (max(fbyje) for fdepartname2 in (' + @sql1 + ') ) c