首页 > 代码库 > 生产库自动派送报表派送失败之重新派送

生产库自动派送报表派送失败之重新派送

/*

Description: 生产库自动派送报表派送失败之重新派送   

*/

 

USE [DB]      --Replace yourDB

GO

 

DECLARE    @ProductDate      nvarchar(10) = N‘2014-01-01‘

DECLARE       @FactoryCode      nvarchar(10) = N‘X‘

 

DECLARE @strSQL          nvarchar(3000)

DECLARE @strCRLF     nvarchar(2)

DECLARE @strCmdShell nvarchar(255)

 

DECLARE @pFactoryName    nvarchar(50)

DECLARE @pSubjectText    nvarchar(255)

DECLARE @pBodyText       nvarchar(max)

DECLARE    @pRecipients  nvarchar(max)

DECLARE @Attachments nvarchar(max)

 

DECLARE @RetVal          int

 

DECLARE @is_master_dw    int

DECLARE @SQLMailProfile  nvarchar(255)

 

DECLARE @template_path      nvarchar(255)

DECLARE @template_name      nvarchar(255)

DECLARE @report_target_name nvarchar(255)

DECLARE @report_full_path   nvarchar(255)

 

SET NOCOUNT ON;

 

SET @strCRLF = NCHAR(13) + NCHAR(10)

 

-----------------------------------------------------------------------

PRINT @strCRLF + N‘********** 產生DB 生產日報表並自動發送**********‘

 

--預設是當天的日報表

IF @ProductDate = N‘‘ SET @ProductDate = LEFT(CONVERT(nvarchar(20), GETDATE(), 120), 10)

--IF@ProductDate = N‘‘ SET @ProductDate = LEFT(CONVERT(nvarchar(20), DATEADD(DD,-1, GETDATE()), 120), 10)

 

--未指定工廠代碼就是全部

IF @FactoryCode = N‘‘ SET @FactoryCode = NULL

 

/* 讀取系統參數*/

SET @is_master_dw = CAST(dbo.fn_GetParameter(N‘@is_master_dw‘) AS int)

SET @SQLMailProfile  = CAST(dbo.fn_GetParameter(N‘@sqlmail_profile‘) AS nvarchar)

   

SET @template_path   = CAST(dbo.fn_GetParameter(N‘@template_path‘) AS nvarchar)

SET @template_name   = CAST(dbo.fn_GetParameter(N‘@template_name‘) AS nvarchar)

SET @report_target_name  = CAST(dbo.fn_GetParameter(N‘@report_target_name‘)AS nvarchar)

 

SET @report_full_path = @template_path + N‘\‘ + @report_target_name

 

   

--以下是自動派送EMAIL 給各收件人

--SET@pRecipients = dbo.fn_GetRecipients()

SET @pRecipients = dbo.fn_GetRecipientsByCode(N‘DailyReport‘)

SET @pRecipients += ‘;mail@163.com‘                  --Replaceyour mail address....

 

IF @is_master_dw = 1

    SET @pSubjectText= N‘<<<XXXXXXXX日報表自動派送>>> 生產日期: + @ProductDate

ELSE

BEGIN

    /* 只處理一個廠,取得加工廠名稱*/

    SELECT TOP1 @pFactoryName =FactoryName FROMdbo.T_factory WHERE IsActive = 1

    SET @pSubjectText= N‘<<<‘+ @pFactoryName+ N‘生產狀況日報表自動派送>>> 生產日期: +@ProductDate

END

 

 

SET @pBodyText  = N‘Dear All + @strCRLF+ @strCRLF

 

SET @pBodyText += N‘附件為 +@ProductDate + N‘ 的生產狀況日報表,請參閱。 + @strCRLF

IF @is_master_dw =1 SET @pBodyText+= N‘如需其他生產數據查詢,請進本公司XXXX 系統查詢,網址為http://XXXX.COM.CN‘ +@strCRLF + @strCRLF

SET @pBodyText += @strCRLF

 

--SET@Attachments = N‘D:\DB\DBDailyReport.xls‘

SET @Attachments = @report_full_path

 

PRINT @pRecipients

PRINT @pBodyText

PRINT @pSubjectText

PRINT @Attachments

 

EXEC msdb.dbo.sp_send_dbmail

    @profile_name =@SQLMailProfile,

    @recipients = @pRecipients,

    @body =@pBodyText,

    @subject =@pSubjectText,

    @file_attachments = @Attachments

 

/*

 * 查询邮件失败之项目

USE msdb

GO

SELECT * FROMsysmail_account

SELECT * FROMsysmail_allitems ORDER BY mailitem_id DESC    --sent_status

*/


本文出自 “畅想天空” 博客,请务必保留此出处http://kinwar.blog.51cto.com/3723399/1548847

生产库自动派送报表派送失败之重新派送