现在有一个表table:id,info,email,enddate
字段名,类型
id int;
info int;
email char;
enddate datetime;
我现在希望当enddate与“今天”日期相差为3天(到期前3天)时取其email自动发送一封邮件提醒用户,并update info为"1",这样的功能用存贮过程+作业的方式是否可以完成,如果可以,具体怎么做呢,代码怎么写?
是否还有其他更好的解决办法?
---------------------------------------------------------------
建存付过程(设表名为ATABLE),然后在JOB中调用
CREATE PROCEDURE dbo.usp_Autosendmail
AS
Declare @Email Varchar(50)
If Exists (Select * from Atable where DateDiff(day,enddate,Getdate())=3 )
begin
declare Tempcur cursor
for Select Email from Atable where DateDiff(day,enddate,Getdate())=3
Open Tempcur
fetch next from Tempcur into @Email
While @@Fetch_status=0
begin
----发EMAIL
Exec master.dbo.xp_SendMail @message='Notice',@recipients=@Email, @subject='Notice',@width=300
fetch next from Tempcur into @Email
end
close Tempcur
deallocate tempcur
---修改标识
Update Atable set Info=1 where DateDiff(day,enddate,Getdate())=3
end
Return