SQL Server在查询结果不为空时发送电子邮件

问题描述 投票:-1回答:1

当查询结果集保存记录时,我需要从SQL Server发送电子邮件。查询可以基于很多逻辑,在几个表之间有连接。

请发送给我正确的方向(视图,视图触发器,SQL Server代理程序作业..?)。

sql-server sql-server-2012 triggers job-scheduling
1个回答
1
投票

使用此处记录的sp_send_DBmail(https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql?view=sql-server-2017)获取所有参数选项

declare @bodytext varchar(max)= '<b>Hey look I wrote something</b>'

if(Exists(select 1 from ....))
begin
EXEC msdb.dbo.sp_send_dbmail  
@recipients='[email protected]',
@subject='ATTN! There are records',
@body=@bodytext,
@body_format='HTML',
@from_address='DBA <[email protected]>',
@reply_to='[email protected]'
end
© www.soinside.com 2019 - 2024. All rights reserved.