我有一个动态的透视 sql 脚本(透视列是动态的)。我想将结果集导出到 excel 文件中,并通过 ssis 中的发送邮件任务通过电子邮件发送它。有谁知道这是怎么做到的吗?下面是我的动态列数据透视 sql 脚本
Declare @SQL varchar(max)
Select @SQL = Stuff((Select Distinct ',' + QuoteName([Response Code]) From YourTable Order by 1 For XML Path('')),1,1,'')
Select @SQL = 'Select [Employee],' + @SQL + '
From (
Select [Employee],[Response Code],Cnt=1,Lvl=0 from YourTable
Union All
Select [Employee],[Response Code],Cnt=0,Lvl=0 from (Select Distinct [Employee] from YourTable) A Join (Select Distinct [Response Code] from YourTable) B on 1=1
Union All
Select ''Total'',[Response Code],count(*),1 From YourTable Group By [Response Code]
) A
Pivot (sum(Cnt) For [Response Code] in (' + @SQL + ') ) p'
Exec(@SQL);
上面的脚本会像这样返回表格
Employee ptb ulm vml wrn
Emp A 0 0 2 1
Emp B 0 2 0 1
Emp C 1 0 1 0
Total 1 2 3 2
我需要将上述结果表导出到 excel 文件中。如果使用 SSIS 列是静态的,我知道该怎么做;但我正在努力处理动态列枢轴。谁能帮帮我。非常感谢您的时间和帮助