Tuesday, 2 July 2013

Automatic email sending in sqlserver

USE [dbMLMWebsite]
GO
/****** Object:  StoredProcedure [dbo].[spMail10days]    Script Date: 07/02/2013 17:20:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[spMail10days]
as
declare @To nvarchar(max);
declare @CC nvarchar(max);
declare @Subject nvarchar(max);
declare @body nvarchar(max);

declare @accountid bigint;
declare @duedate nvarchar(50);
declare @email nvarchar(50);
declare @sid int;
declare @getsid cursor;
Begin
set @getsid = cursor for
select accountid from tblMemberPaymentInfo where DATEDIFF(DAY, GETDATE(), duedate )=10
open @getsid
fetch next
from @getsid into @sid
print @sid
while @@fetch_status = 0
Begin
BEGIN
SET NOCOUNT ON;
-- innerjoin query
select @duedate=a.duedate,@email=b.email
from tblMemberPaymentInfo a inner join tblRegistrationInfo b
on a.accountid=b.accountid where a.accountid=@sid
--append body
--@body='Event NAme:' +@Eventname+'<br/>'
set @body=@body+'Due Date : '+@duedate+'<br/>'
print @body
EXEC msdb.dbo.sp_send_dbmail @profile_name ='MailSending',
@recipients=@email ,  @blind_copy_recipients=@CC,
@body = @body, @body_format = 'HTML',@subject = 'hi'
END
fetch next
from @getsid into @sid
end
close @getsid
deallocate @getsid
end
GO

No comments:

Post a Comment