Home  >  Article  >  Database  >  How to send SQL Server stored procedure emails in format (code example)

How to send SQL Server stored procedure emails in format (code example)

不言
不言forward
2019-01-12 11:16:445127browse

The content of this article is about the method of sending SQL Server stored procedure emails in format (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

1. The message received shows:

2. The stored procedure code part:

BEGIN
    SET NOCOUNT ON;
    --初始化
        Declare @MailTo         nvarchar(max)
        Declare @MailCc         nvarchar(max)
        Declare @MailBcc        nvarchar(max)
        Declare @MailSubject    nvarchar(255)
        Declare @MailBody       nvarchar(max)
        Declare @MailFormat     nvarchar(20)
        DECLARE @SignCount      nvarchar(50)
    --查询表格中异常资料笔数
    SELECT @SignCount=COUNT(1) FROM  表名称  WHERE  筛选条件 

        SET @MailSubject='異常共'+@SignCount+'筆'
        SET @MailFormat='HTML'
        SET @MailTo='xxx.zzz@qq.com'    --收件人邮件地址
        SET @MailCc='xxx.zzz@qq.com'   --抄送
        SET @MailBcc='xxx.zzz@qq.com'  --密送
        SET @MailBody=''    --发送内容

            SET @MailBody =

                    N'Dear 
Sir/Madam:<br>異常共&#39;+@SignCount+&#39;筆,請及時核對,謝謝!!!<br><H4>    列表如下:</H4>&#39;
 +
     --定义邮件表格尺寸大小
                    N&#39;<table border="1" style="font-size:11px;text-align:center"  width="60%">&#39; +     
    --定义列表对应列名称 
                    N&#39;<tr style="font-size:11"><th>工號</th>&#39; +    
                    N&#39;<th>姓名</th>&#39; +
                    N&#39;<th>已用天數</th>&#39; +
                    N&#39;<th>實際已用天數</th>&#39; +
                    N&#39;<th>異常狀態</th>&#39; +
    --表主体结果
                    CAST ( ( SELECT 
                                                        td = Empid, &#39;&#39;,
                                                        td = EmpCName, &#39;&#39;,
                                                        td = [ULColA29-AdjAnlUsePos-Sum], &#39;&#39;,
                                                        td = VocaTotalHours, &#39;&#39;,
                                                        td = [ULColA29-State], &#39;&#39;
                                FROM  表名称  Form   WHERE  筛选条件   FOR XML PATH(&#39;tr&#39;), TYPE     --将查询出来结果以HTML语言 td/tr显示出来
                                ) AS NVARCHAR(MAX)
       ) +
                            N&#39;</table><br>以上為系統發送,請勿回復!!!<br>xx部xx課&#39; 

              declare @i int   
			  BEGIN
				    SET @i=0;

				    EXEC asdb.dbo.xx_xxx_xxxxx      --执行SQL邮件发送的一个功能地址,这个地址就要去问公司的负责人了
    --对应的邮件主体参数带入发送邮件
				    @profile_name=&#39;MIS_SMTP_Mail&#39;,     --系统设置的邮件发送名称    --公司邮件管理者DB设置
				    @recipients=@MailTo,
				    @copy_recipients=@MailCc,
				    @blind_copy_recipients=@MailBcc,
				    @subject=@MailSubject,
				    @body=@MailBody,
				    @body_format=@MailFormat

				    SET @i=1;
			  END
END

The above is the detailed content of How to send SQL Server stored procedure emails in format (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete