使用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
替换我的查询,然后我的附件有数据。
什么可能导致我的查询没有返回任何数据?
答案 0 :(得分: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 = ' '
;