SQL DATABASE SEND MAIL EXAMPLE SQL发送邮件实例

USE [mydatabase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:        John Tse
-- Create date: <Create Date,,>
-- Description:    <Description,,> Alert mail for power meter loss connection
-- =============================================
Alter PROCEDURE [dbo].[EMS_Mail_PowerMeterLSC]

AS
BEGIN

SET NOCOUNT ON;

declare @mcount int
set @mcount = 0
select @mcount = count(*)
FROM [myserver01].[EMS].[dbo].[PowerMeter]
where datediff(MINUTE,LastUpdateTime, getdate()) > 15 
--select @mcount
if @mcount =  0
begin
    --print @mcount
    return
end

DECLARE @value VARCHAR(50)
DECLARE @xml NVARCHAR(MAX)
DECLARE @xml2 NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
Declare @MailSubject Nvarchar(100)
   
    SET @xml2 =CAST((
        select PMCode as 'td','',PMName as 'td','',[Current] as 'td','',[VoltageLL] as 'td','',[VoltageLN] as 'td','',[ActivePower] as 'td','',[PowerFactor] as 'td','',[AccEnergy] as 'td','',[LastUpdateTime] as 'td'
        FROM [Strzssvprd01].[EMS].[dbo].[PowerMeter] where datediff(MINUTE,LastUpdateTime, getdate()) > 15
        FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

    --print @xml2

     begin 
        SET @body ='<html>
        <style>
                                table.customTable {
  
                                  background-color: #FFFFFF;
                                  border-collapse: collapse;
                                  border-width: 2px;
                                  border-color: #7EA8F8;
                                  border-style: solid;
                                  color: #000000;
                                }

                                table.customTable td, table.customTable th {
                                  border-width: 2px;
                                  border-color: #7EA8F8;
                                  border-style: solid;
                                  padding: 5px;
                                }

                                table.customTable thead {
                                  background-color: red;
                                  color: yellow;
                                }
                                </style>                            
                            <body>
                                <table  class="customTable"> 
                                <thead>
                                <th> PMCode </th> 
                                <th> PMName </th> 
                                <th> Current </th>
                                <th> VoltageLL </th>
                                <th> VoltageLN </th>
                                <th> ActivePower </th>
                                <th> PowerFactor </th>
                                <th> AccEnergy </th>
                                <th> LastUpdateTime </th>
                                </thead>'  
        SET         @body = @body + @xml2 +'</table></body></html>

        SET            @mailSubject = ' EMS Power Meters loss connection list ' +CONVERT(VARCHAR(10), DATEADD(day, DATEDIFF(day, 0, GETDATE()-1), 0) , 111)
       
        EXEC         msdb.dbo.sp_send_dbmail 

                    @profile_name='Mail PROFILE NAME',
                    @recipients    = 'JohnT@163.com',-- @RecipientMB, 
                    --@blind_copy_recipients=    'JohnT@163.com;',
                    @body = @body,
                    @body_format='HTML',
                    @subject= @mailsubject;    
        end
END   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值