我可以从#temp table select 定义dbmail 的@query 属性吗?

Can I select from a #temp table to define the @query attribute to dbmail?

当我尝试将查询附加为电子邮件附件时出现错误。查询来自临时 table。电子邮件没有附件也能正常工作。

我尝试添加@query_result_header = 1,这是针对此错误的建议修复。

执行 msdb.dbo.sp_send_dbmail @profile_name = 'data.production',

.......

,@query = 'select *
          from #pcpInfo
          order by pcp',
@attach_query_result_as_file=1,
@query_attachment_filename = 'MemberAttrition.csv',
@query_result_separator = ',',
@query_result_header = 1

我原以为电子邮件会附有一个 csv 文件,但我却收到以下输出错误 - "Failed to initialize sqlcmd library with error number -2147467259." 有什么建议吗?

像这样的东西应该可以工作:

Create table ##pcpInfo
(
    ID int
)
Insert into ##pcpInfo (ID)
Values (1)

exec msdb.dbo.sp_send_dbmail @profile_name = 'data.production',
@recipients = 'Email@Email.com'
,@query = 'select id
          from ##pcpInfo',
@attach_query_result_as_file=1,
@query_attachment_filename = 'MemberAttrition.csv',
@query_result_separator = ',',
@query_result_header = 1,
 @exclude_query_output = 0


Drop Table ##pcpInfo

这正在将您的温度 table 更改为全局温度 table,我已经测试过它并且对我有效。

编辑

您还需要删除 order 子句。