SQL通过电子邮件发送作业

时间:2018-02-26 20:10:07

标签: sql-server tsql sp-send-dbmail

使用预定作业发送电子邮件的最佳方法是什么?现在我们这样做的方式很难。附加Excel电子表格只有在满月时才有效,直接数据看起来很神秘。有没有更好的办法?我正在使用SSMS 17.5有人说我们这样做的方式已经过时了。有什么更好的方法呢?我想SSMS我们有点卡住了。谢谢。

这是一个小例子:

GO
/****** Object:  StoredProcedure [dbo].[EMAIL_HoursReport]    Script Date: 2/26/2018 2:03:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[EMAIL_HoursReport]   --exec EMAIL_HoursReport @nvRecipients  = 'me@gmail.com'

(
    @nvRecipients as varchar(1000)
)
AS

Declare @nvMessage nvarchar(1000)
Declare @nvSubject nvarchar(1000)
Declare @nvQuery nvarchar(4000)
Declare @iWidth int
Set @iWidth = 4000
Set @nvSubject = ' Hours Report ' 

CREATE TABLE #Data
(
  Hours varchar(50)
, FirstName varchar(50)
, LastName varchar(50)
)

INSERT INTO #Data
SELECT Hours, FirstName, LastName FROM EmpData
WHERE CAST(DateEntered as datetime) = CAST(CONVERT(CHAR(16), GETDATE(),112) AS datetime)
AND Cast(Hours as float) > 10

IF EXISTS (SELECT * FROM #Data)
    BEGIN   
        SET @nvquery = 'SELECT Hours, FirstName, LastName FROM #Data' 
        SET @nvMessage = 'Hours Report ' + isnull(' - (' + db_name() + '.dbo.' + object_name(@@procid) + ')', '')
        EXEC msdb.dbo.sp_send_dbmail_mod
            @recipients = @nvRecipients,
            @subject = @nvSubject,
            @body = @nvMessage,
            @query = @nvQuery
    END
    DROP TABLE #Data

0 个答案:

没有答案