[MSSQL]SQL查询结果转换成HTML并通过邮件预警

上一篇文章讲解到配置数据库邮件实现发送邮件功能。

邮件的内容为HTML格式。
案例:

-- =============================================
-- Author:      
-- Create date: Date,2017-08-20,>
-- Description: 
-- =============================================
ALTER TRIGGER [dbo].[TR_NOROHS] 
   ON  [dbo].[PURTD]
   AFTER INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

DECLARE @tableHTML  NVARCHAR(MAX) ;
if exists(
    SELECT      td = RTRIM(TC001), '',
                    td = RTRIM(TC002), '',
                    td = RTRIM(TD003), '',
                    td = RTRIM(TC004), '',
                    td = RTRIM(MA003), '',
                    td = RTRIM(TD004), '',
                    td = RTRIM(TD005), '',
                    td = RTRIM(TD006), '',
                    td = RTRIM(MV002), ''                        
              from   inserted 
INNER JOIN PURTC on TD001=TC001 AND TD002=TC002
INNER JOIN PURMA ON TC004=MA001
INNER JOIN CMSMV ON TC011=MV001
LEFT JOIN ROSH ON RTRIM(TC004)=Supplier AND  TD004=Item
WHERE ROSH.Item IS NULL
             )
begin
SET @tableHTML =
    N'

无ROHS报告材料清单

' +
N'' + N'' + N''+ CAST ( ( SELECT td = RTRIM(TC001), '', td = RTRIM(TC002), '', td = RTRIM(TD003), '', td = RTRIM(TC004), '', td = RTRIM(MA003), '', td = RTRIM(TD004), '', td = RTRIM(TD005), '', td = RTRIM(TD006), '', td = RTRIM(MV002), '' from inserted INNER JOIN PURTC on TD001=TC001 AND TD002=TC002 INNER JOIN PURMA ON TC004=MA001 INNER JOIN CMSMV ON TC011=MV001 LEFT JOIN ROSH ON RTRIM(TC004)=Supplier AND TD004=Item WHERE ROSH.Item IS NULL FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) + N'
单别单号序号供应商编码供应商品号品名规格采购人员
' ;
--print @tableHTML EXEC msdb.dbo.sp_send_dbmail @profile_name = 'MailETL', @recipients='[email protected]', @blind_copy_recipients = '[email protected]' , @subject = '新增采购单无ROHS报告预警信息', @body = @tableHTML, @body_format = 'HTML' ; end -- Insert statements for trigger here END

[MSSQL]SQL查询结果转换成HTML并通过邮件预警_第1张图片

感谢小叶的帮助

你可能感兴趣的:(数据库管理(ORC/SQL),ERP-鼎捷)