0

我正在尝试执行以下操作,用户在 aspx 页面上填写发布日期和到期日期。这些日期的yyyy/MM/dd格式保存在数据库中。

我希望能够在截止日期前 3 天向用户发送电子邮件。

我能够找到的方法是使用 a Windows Serviceor SQL Job,但问题是我无法围绕这个概念并让它发挥作用。

令人困惑的部分是我必须为 Windows 服务创建一个新项目,现在我不知道这是否正确,但我的方法是连接到 SQL Server 并使用问题和截止日期查询数据库,然后执行计算并将其存储在 中resultdate,如果resultdate等于Today's date则将电子邮件发送给以下用户,否则不进行轮询。


编辑:

我试图通过创建一个来实现上述目标Stored Procedure,它将设置在Windows Service. 目前,我正在努力解决以下问题:

我有一个MS SQL DB包含 aclosing date的目标是向其电子邮件2 daysclosing date.

请看下面的代码,我正在使用CURSOR FOR但不确定如何对SELECT不同的数据库使用两组命令,而第一组命令SELECT存储在一些变量中,这些变量从另一个数据库中检索余额数据。

USE ROG
DECLARE @Currentdate varchar(10), @DueDate varchar(10), @Serial_No int, @Product_Line varchar(50), @Email varchar(50)
SET @Currentdate = GETDATE()

DECLARE RFQDate CURSOR FOR

SELECT Serial_No, RFQ_Closing_Date, Product_Line FROM RFQDB WHERE DATEDIFF(day, GETDATE(), RFQ_Closing_Date) = 2

OPEN RFQDate

FETCH NEXT FROM RFQDate INTO @Serial_No, @DueDate, @Product_Line 

WHILE @@FETCH_STATUS = 0

BEGIN 

FETCH NEXT FROM RFQDate INTO @Serial_No, @DueDate, @Product_Line

PRINT ' Due Date ' + @DueDate + ' Serial No ' + coalesce(Convert(varchar(4), @Serial_No),'') + ' Product Line ' + @Product_Line 

END

DECLARE EMAIL CURSOR FOR     --Not sure if this way is correct (this is where I need help!!!

SELECT Sales_Email_ID FROM ProductLineDB WHERE Product_Line = @Product_Line

OPEN EMAIL

FETCH NEXT FROM EMAIL INTO @Email

WHILE @@FETCH_STATUS = 0

BEGIN 

FETCH NEXT FROM EMAIL INTO @Email

PRINT ' EMAIL ' + @Email

END

CLOSE EMAIL

DEALLOCATE EMAIL

CLOSE RFQDate

DEALLOCATE RFQDate

但是,如果您测试上述代码,它将正确显示所有到期日期,但仅显示与到期日期关联的 1 封电子邮件。相反,它应该显示 5 封电子邮件。

MS SQL 消息屏幕

Due Date 2013/02/14 Serial No 1015 Product Line Tubes
Due Date 2013/02/14 Serial No 1015 Product Line Instrumentation
Due Date 2013/02/14 Serial No 1015 Product Line Tooling
Due Date 2013/02/14 Serial No 1015 Product Line Tooling
Due Date 2013/02/14 Serial No 1015 Product Line Tooling
EMAIL xxx@abc.com

编辑正确的代码

以下代码正在获取我需要的所有信息,现在我只需要实现从标量值sp_send_dbmail中获取。@recipients@Email

The working code is as the following:

USE ROG
DECLARE @Currentdate varchar(10), @DueDate varchar(10), @Serial_No int, @Product_Line varchar(50), @Email varchar(50)
SET @Currentdate = GETDATE()

DECLARE RFQDate CURSOR FOR

SELECT Serial_No, RFQ_Closing_Date, Product_Line FROM RFQDB WHERE DATEDIFF(day, GETDATE(), RFQ_Closing_Date) = 2

OPEN RFQDate

FETCH NEXT FROM RFQDate INTO @Serial_No, @DueDate, @Product_Line 

WHILE @@FETCH_STATUS = 0

BEGIN 

DECLARE EMAIL CURSOR FOR

SELECT Sales_Email_ID FROM ProductLineDB WHERE Product_Line = @Product_Line

OPEN EMAIL

FETCH NEXT FROM EMAIL INTO @Email

PRINT ' EMAIL: ' + @Email

WHILE @@FETCH_STATUS = 0

BEGIN 

FETCH NEXT FROM EMAIL INTO @Email 

END

CLOSE EMAIL

DEALLOCATE EMAIL

FETCH NEXT FROM RFQDate INTO @Serial_No, @DueDate, @Product_Line

PRINT ' Due Date: ' + @DueDate + ' Serial No: ' + coalesce(Convert(varchar(4), @Serial_No),'') + ' Product Line: ' + @Product_Line 

END

CLOSE RFQDate

DEALLOCATE RFQDate
4

1 回答 1

0

工作代码如下:

USE ROG
DECLARE @Currentdate varchar(10), @DueDate varchar(10), @Serial_No int, @Product_Line varchar(50), @Email varchar(50), @RFQ_No bigint, @Subject nvarchar(500), @Profile_No int, @Body nvarchar(500), @copy_recipients varchar(max) = 'abc@xyz; dfg@xyz'
SET @Currentdate = GETDATE()

DECLARE RFQDate CURSOR FOR

SELECT Serial_No, RFQ_No, RFQ_Closing_Date, Product_Line FROM RFQDB WHERE DATEDIFF(day, GETDATE(),       RFQ_Closing_Date) = 2

OPEN RFQDate

FETCH NEXT FROM RFQDate INTO @Serial_No, @RFQ_No, @DueDate, @Product_Line 

WHILE @@FETCH_STATUS = 0

BEGIN 

DECLARE EMAIL CURSOR FOR

SELECT Profile_No, Sales_Email_ID FROM ProductLineDB WHERE Product_Line = @Product_Line

OPEN EMAIL

FETCH NEXT FROM EMAIL INTO @Profile_No, @Email

PRINT ' EMAIL: ' + @Email + ' | Profile No: ' + coalesce(Convert(varchar(4), @Profile_No),'')

WHILE @@FETCH_STATUS = 0

BEGIN 

FETCH NEXT FROM EMAIL INTO @Profile_No, @Email 

END

CLOSE EMAIL

DEALLOCATE EMAIL

FETCH NEXT FROM RFQDate INTO @Serial_No, @RFQ_No, @DueDate, @Product_Line

SET @Subject = ' Profile No: ' + coalesce(Convert(varchar(4), @Profile_No),'') + ' | RFQ No: ' +  coalesce(Convert(nvarchar(10), @RFQ_No),'') + ' | Due Date: ' + @DueDate 

SET @Body = 'The following ' + ' RFQ No: ' +  coalesce(Convert(nvarchar(10), @RFQ_No),'') + ' is due on date: ' + @DueDate 

PRINT ' Due Date: ' + @DueDate + ' | Serial No: ' + coalesce(Convert(varchar(4), @Serial_No),'') + ' | Product Line: ' + @Product_Line 

PRINT @Body

PRINT @Subject

IF @Email != ''
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = '31BM',
@importance = 'High',
@recipients = @Email,
@copy_recipients = @copy_recipients,
@subject = @Subject,
@body = @Body;
END
ELSE
PRINT 'No Email to send'
END

CLOSE RFQDate

DEALLOCATE RFQDate

以上是最终的工作代码,我决定SQL SERVER AGENT每天用它来执行这项工作。

我希望它有所帮助。

于 2013-02-12T20:21:41.487 回答