使用这样的sp_send_dbmail
时:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MY_PROFILE'
,@recipients = 'MY_EMAIL_ADDRESS'
,@query = 'SELECT TOP 50 FIELD1, FIELD2, FIELD3 FROM TABLE1'
,@subject = 'MY_SUBJECT'
,@body = 'This is a test'
,@attach_query_result_as_file = 1
,@query_attachment_filename = 'file.csv'
,@query_result_separator = ' '
;
附加的文件始终为空。我在sp_send_dbmail
之外尝试了查询,效果很好。我还尝试用SELECT 1
替换我的查询,然后我的附加文件有数据。
什么可能导致我的查询不返回任何数据?
我找到了解决方法。我必须在表名之前指定数据库。更像是:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MY_PROFILE'
,@recipients = 'MY_EMAIL_ADDRESS'
,@query = 'SELECT TOP 50 FIELD1, FIELD2, FIELD3 FROM DATABADE.dbo.TABLE1'
,@subject = 'MY_SUBJECT'
,@body = 'This is a test'
,@attach_query_result_as_file = 1
,@query_attachment_filename = 'file.csv'
,@query_result_separator = ' '
;