当查询结果集保存记录时,我需要从SQL Server发送电子邮件。查询可以基于很多逻辑,在几个表之间有连接。
请发送给我正确的方向(视图,视图触发器,SQL Server代理程序作业..?)。
使用此处记录的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