如何设置 sp_send_dbmail 将查询结果通过邮件发送?
How can I set sp_send_dbmail to email the query results?
我的任务是创建一份离职用户列表,我们需要将其存档在内部应用程序中。
我已经编写了 select 语句 (SQL Server 2012),它运行良好。我可以从 EXEC sp_send_dbmail
获取要发送的电子邮件,但电子邮件只有我放在正文中的那一行。当我更改 @body
信息时,它仍然只使用我输入的原始行。它没有改变我在那里添加的内容,我无法让它发送 Select
.
的结果
这是我目前的情况:
Set @Results = 'SELECT p.UID
,p.EMPNO
,p.FULLNAME AS FULL_NAME
,p.Archived
,p.COMPANY
,p.EMAIL
,p.SID
,e.TERMCODE AS TERM_CODE
,CONVERT(VARCHAR, e.FIREDATE, 101) AS FIRE_DATE
FROM app_table p
LEFT OUTER JOIN employee_table e ON e.EMPNO=p.EMPNO
WHERE p.EMPNO IS NOT NULL
AND p.ARCHIVED = 0
AND e.FIREDATE IS NOT NULL
ORDER BY e.FIREDATE DESC'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL_Mailer',
@recipients = 'my.address@myemail.com',
@subject = 'Departed Users being archived',
@body = @Results
END
我也试过将 @query
添加到 sp_send_dbmail
并将 Select
放入 @query
:@query = 'Select statement above'
我错过了什么?
您必须在 @query
参数中传递查询并指定 @attach_query_result_as_file=1
。 @body
只是要包含在消息中的文本。有关详细信息,请参阅 sp_send_dbmail。你说你试过了,但没有说发生了什么,你的代码也没有那样做。
Set @Results = 'SELECT p.UID
,p.EMPNO
,p.FULLNAME AS FULL_NAME
,p.Archived
,p.COMPANY
,p.EMAIL
,p.SID
,e.TERMCODE AS TERM_CODE
,CONVERT(VARCHAR, e.FIREDATE, 101) AS FIRE_DATE
FROM app_table p
LEFT OUTER JOIN employee_table e ON e.EMPNO=p.EMPNO
WHERE p.EMPNO IS NOT NULL
AND p.ARCHIVED = 0
AND e.FIREDATE IS NOT NULL
ORDER BY e.FIREDATE DESC'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL_Mailer',
@recipients = 'my.address@myemail.com',
@subject = 'Departed Users being archived',
@body = 'You can write something here',
@query = @Results,
@attach_query_result_as_file = 1
我的任务是创建一份离职用户列表,我们需要将其存档在内部应用程序中。
我已经编写了 select 语句 (SQL Server 2012),它运行良好。我可以从 EXEC sp_send_dbmail
获取要发送的电子邮件,但电子邮件只有我放在正文中的那一行。当我更改 @body
信息时,它仍然只使用我输入的原始行。它没有改变我在那里添加的内容,我无法让它发送 Select
.
这是我目前的情况:
Set @Results = 'SELECT p.UID
,p.EMPNO
,p.FULLNAME AS FULL_NAME
,p.Archived
,p.COMPANY
,p.EMAIL
,p.SID
,e.TERMCODE AS TERM_CODE
,CONVERT(VARCHAR, e.FIREDATE, 101) AS FIRE_DATE
FROM app_table p
LEFT OUTER JOIN employee_table e ON e.EMPNO=p.EMPNO
WHERE p.EMPNO IS NOT NULL
AND p.ARCHIVED = 0
AND e.FIREDATE IS NOT NULL
ORDER BY e.FIREDATE DESC'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL_Mailer',
@recipients = 'my.address@myemail.com',
@subject = 'Departed Users being archived',
@body = @Results
END
我也试过将 @query
添加到 sp_send_dbmail
并将 Select
放入 @query
:@query = 'Select statement above'
我错过了什么?
您必须在 @query
参数中传递查询并指定 @attach_query_result_as_file=1
。 @body
只是要包含在消息中的文本。有关详细信息,请参阅 sp_send_dbmail。你说你试过了,但没有说发生了什么,你的代码也没有那样做。
Set @Results = 'SELECT p.UID
,p.EMPNO
,p.FULLNAME AS FULL_NAME
,p.Archived
,p.COMPANY
,p.EMAIL
,p.SID
,e.TERMCODE AS TERM_CODE
,CONVERT(VARCHAR, e.FIREDATE, 101) AS FIRE_DATE
FROM app_table p
LEFT OUTER JOIN employee_table e ON e.EMPNO=p.EMPNO
WHERE p.EMPNO IS NOT NULL
AND p.ARCHIVED = 0
AND e.FIREDATE IS NOT NULL
ORDER BY e.FIREDATE DESC'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL_Mailer',
@recipients = 'my.address@myemail.com',
@subject = 'Departed Users being archived',
@body = 'You can write something here',
@query = @Results,
@attach_query_result_as_file = 1