根据 T-SQL 存储过程的结果,通过 SQL 服务器代理发送电子邮件
Send email via SQL Server Agent, based on the result of a T-SQL stored procedure
我有一个 T-SQL 存储过程(其中 returns 一个称为 @HourDifference
的单个标量值作为输出参数);你可以看到下面的执行:
DECLARE @HourDifference_output TINYINT;
-- I declare a variable to capture the output parameter--
EXEC dbo.sproc_XYZ_Notification
@HourDifference = @HourDifference_output OUTPUT;
SELECT @HourDifference_output AS HourDifferenceCaptured
我有以下要求:
如果HourDifferenceCaptured > 12
,我需要发邮件
如果HourDifferenceCaptured <= 12
,则不需要发送邮件;什么都不用做。
我需要在 SQL 服务器代理中设置两个时间表,一个在早上 7 点,另一个在晚上 7 点。
有人可以提供代码并指导我完成这个过程吗?
USE <database_name>
GO
DECLARE @subject NVARCHAR(max) = 'Email subject'
, @body NVARCHAR(MAX)
DECLARE @HourDifference_output TINYINT;
EXEC dbo.sproc_XYZ_Notification @HourDifference = @HourDifference_output OUTPUT;
SELECT @HourDifference_output AS HourDifferenceCaptured
IF (@HourDifference_output> 12)
BEGIN
SET @body = '<!DOCTYPE html>
<html>
<body>
<body style="color:black; font-family:Times New Roman; font-size:14x"> .......... body text ......... </body>
<body style="color:SlateGray; font-family:Times New Roman; font-size:14px;line-height: 1;"> ------------------------------------------------------------------' + '
<body style="color:SlateGray; font-family:Times New Roman; font-size:14px;line-height: 1;"> ------------------------------------------------------------------' + '
</body>
</HTML>'
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Profile name'
, @recipients = 'distibution email'
, @body = @body
, @body_format = 'HTML'
, @subject = @subject
END
GO
您可以创建一个 SQL 服务器代理作业,在需要时使用 t-sql 步骤使用 msdb.dbo.sp_send_dbmail
发送电子邮件(请参阅 here, 存储过程完整参考)。
尝试类似于以下的操作:
DECLARE @HourDifference_output TINYINT;
EXEC dbo.sproc_XYZ_Notification @HourDifference_output OUTPUT;
-- SELECT @HourDifference_output AS HourDifferenceCaptured
IF @HourDifference_output > 12
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'db_mail_profile_i_have_already_created',
@recipients = 'intended-recipients@yourorganization.com',
@body = 'Too many hours difference.',
@subject = 'Automated Message' ;
END
您必须已经配置了 database mail account, a database mail profile 并授予用户 运行 作业步骤的适当访问权限。第二个 link 还包含用于创建数据库邮件帐户和配置文件、将帐户添加到配置文件并适当授予访问权限的示例脚本(我个人更喜欢通过 SSMS 数据库邮件向导配置数据库邮件)。
要做出的决定是创建 public 还是私人个人资料。您可以找到有关差异的更多信息 here.
最后,在我看来,当作业/步骤失败时通知管理员(通过 SQL 服务器代理内置机制)是一个很好的做法。
我有一个 T-SQL 存储过程(其中 returns 一个称为 @HourDifference
的单个标量值作为输出参数);你可以看到下面的执行:
DECLARE @HourDifference_output TINYINT;
-- I declare a variable to capture the output parameter--
EXEC dbo.sproc_XYZ_Notification
@HourDifference = @HourDifference_output OUTPUT;
SELECT @HourDifference_output AS HourDifferenceCaptured
我有以下要求:
如果
HourDifferenceCaptured > 12
,我需要发邮件如果
HourDifferenceCaptured <= 12
,则不需要发送邮件;什么都不用做。
我需要在 SQL 服务器代理中设置两个时间表,一个在早上 7 点,另一个在晚上 7 点。
有人可以提供代码并指导我完成这个过程吗?
USE <database_name>
GO
DECLARE @subject NVARCHAR(max) = 'Email subject'
, @body NVARCHAR(MAX)
DECLARE @HourDifference_output TINYINT;
EXEC dbo.sproc_XYZ_Notification @HourDifference = @HourDifference_output OUTPUT;
SELECT @HourDifference_output AS HourDifferenceCaptured
IF (@HourDifference_output> 12)
BEGIN
SET @body = '<!DOCTYPE html>
<html>
<body>
<body style="color:black; font-family:Times New Roman; font-size:14x"> .......... body text ......... </body>
<body style="color:SlateGray; font-family:Times New Roman; font-size:14px;line-height: 1;"> ------------------------------------------------------------------' + '
<body style="color:SlateGray; font-family:Times New Roman; font-size:14px;line-height: 1;"> ------------------------------------------------------------------' + '
</body>
</HTML>'
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Profile name'
, @recipients = 'distibution email'
, @body = @body
, @body_format = 'HTML'
, @subject = @subject
END
GO
您可以创建一个 SQL 服务器代理作业,在需要时使用 t-sql 步骤使用 msdb.dbo.sp_send_dbmail
发送电子邮件(请参阅 here, 存储过程完整参考)。
尝试类似于以下的操作:
DECLARE @HourDifference_output TINYINT;
EXEC dbo.sproc_XYZ_Notification @HourDifference_output OUTPUT;
-- SELECT @HourDifference_output AS HourDifferenceCaptured
IF @HourDifference_output > 12
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'db_mail_profile_i_have_already_created',
@recipients = 'intended-recipients@yourorganization.com',
@body = 'Too many hours difference.',
@subject = 'Automated Message' ;
END
您必须已经配置了 database mail account, a database mail profile 并授予用户 运行 作业步骤的适当访问权限。第二个 link 还包含用于创建数据库邮件帐户和配置文件、将帐户添加到配置文件并适当授予访问权限的示例脚本(我个人更喜欢通过 SSMS 数据库邮件向导配置数据库邮件)。
要做出的决定是创建 public 还是私人个人资料。您可以找到有关差异的更多信息 here.
最后,在我看来,当作业/步骤失败时通知管理员(通过 SQL 服务器代理内置机制)是一个很好的做法。