Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL email notification


SQL email notification

Author
Message
sfletcher-933270
sfletcher-933270
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 218
I have developed two stored procedures which work together to generate an email to users depending on an event in the database (if a date has passed). I have based these on successful SQL email notifications using stored procedures that were developed by our database developer.

My stored procedures work well in a test environment. When I execute them myself from within SQL server management studio express, they fire off the expected emails. But when they were scheduled to run in our live database (?using SQL Agent) users received the same email each day, ie a repetition of the same email each day, not a new email depending on the date value as I expected. I have added the scripts for the stored procedures below FYI.

I don't have access to SQL Agent where I believe the job is scheduled to run at the same time each night. I suspect that the problem is there, but if you can see anything wrong with my scripts, or if you can tell me how to prevent this duplication in SQL Agent, I would be very grateful for your advice.

Regards, Sally

--first script
USE [TMS2]
GO
/****** Object: StoredProcedure [dbo].[spANMM_SF_ExpiringInterimReceipt] Script Date: 11/25/2009 12:49:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[spANMM_SF_ExpiringInterimReceipt]
(@ID int)
as
begin
declare @ObjectNumber varchar (64),
@Description varChar(4000),
@Title varchar (255), -- ObjTitles.Title is not null
@EnteredDate datetime,
@Department varchar (64) -- Departments.Department is not null
declare @DepartmentID int
declare @Body1 varchar (8000)
declare @Subject1 varchar (4000)
declare @Style varchar (4000)
declare @Addressee varchar(50)

select @Style = '<style>p{font-family:arial;font-size:9pt;}table{font-family:arial;font-size:9pt;border-collapse:collapse;width:100%;}</style><table cellpadding=5>'

select @ObjectNumber = O.ObjectNumber,
@Description = replace(isnull(cast(O.Description as Varchar(4000)), ''), char(13) + char(10), '<p/>'),
@Title = replace(isnull(cast(OT.Title as Varchar(4000)), ''), char(13) + char(10), '<p/>'),
@EnteredDate = O.EnteredDate,
@Department = D.Department,
@DepartmentID = O.DepartmentID

from Objects O left join Objtitles Ot
on O. ObjectID = Ot.ObjectID
and Ot.DisplayOrder = 1

join Departments D
on O.DepartmentID = D.DepartmentID

Where O.ObjectID = @ID

begin
select @Subject1 = @Department + ' ' + substring (@ObjectNumber, 1,12) + ' ' + 'assessment due'''
select @Body1 = @Style + '

The following interim receipts were received on' + ' ' + convert(varchar,@EnteredDate, 106)+'.' + ' ' + 'Please ensure an assessment to acquire or return this material is made within two weeks of this email.<p/>'

select @Body1 = @Body1 + '

' + cast (@ObjectNumber as varchar) + ' ' + @Title + ''

select @body1 = @Body1 + '

' + @Description + ''

IF @DepartmentID = 1 or @DepartmentID = 4 or @DepartmentID = 7-- Exploration and European Settlement, Australian Naval History and Indigenous Communities
Select @Addressee = 'sfletcher'--'nerskine' -- sfletcher for testing purposes
if @DepartmentID = 2 -- Convicts, Migrants and Refugees
select @Addressee = 'sfletcher'--'ktao'
if @DepartmentID = 3 or @DepartmentID = 12-- Shipping and Trade and Environment and Industry
select @Addressee = 'sfletcher'--'dfletcher'
if @DepartmentID = 5 -- Sport and Lifestyle
select @Addressee = 'sfletcher'--'pcuthbert'
if @DepartmentID = 6 -- Australian American Maritime Experience
select @Addressee = 'sfletcher'--'phundley'
if @DepartmentID = 8 or @DepartmentID = 11 -- Maritime Technology
select @Addressee = 'sfletcher'--'khosty'
if @DepartmentID = 10 --Not Assigned
select @Addressee = 'sfletcher' -- testing

exec sp_send_cdosysmail @from = 'TMSAdmin', @to = @Addressee , @cc = '', @subject = @subject1, @body = @body1

end

end


--second script

USE [TMS2]
GO
/****** Object: StoredProcedure [dbo].[spANMM_SF_SendExpiringInterimReceipts] Script Date: 11/25/2009 12:51:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Script to send email re stored procedure to remind curators of expiring interim receipts

CREATE proc [dbo].[spANMM_SF_SendExpiringInterimReceipts]

As

Declare @ID int

Declare ObjectIDs Cursor For
select O.ObjectID

from Objects O left join Objtitles Ot
on O. ObjectID = Ot.ObjectID
and Ot.displayOrder = 1

join Departments D
on O.DepartmentID = D.DepartmentID

join ObjCurLocView Ocl
on O.ObjectID = OcL.ObjectID

where O.ObjectStatusID = 15
AND
DateDiff(day, O.EnteredDate, getdate()) >=28
and Ocl.curSite = 'WH'


open ObjectIDs

fetch next from ObjectIDs into @ID

While @@fetch_status = 0
Begin

exec spANMM_SF_ExpiringInterimReceipt @ID


fetch next from ObjectIDs into @ID
End

close ObjectIDs
deallocate ObjectIDs
Slick84
Slick84
Mr or Mrs. 500
Mr or Mrs. 500 (512 reputation)Mr or Mrs. 500 (512 reputation)Mr or Mrs. 500 (512 reputation)Mr or Mrs. 500 (512 reputation)Mr or Mrs. 500 (512 reputation)Mr or Mrs. 500 (512 reputation)Mr or Mrs. 500 (512 reputation)Mr or Mrs. 500 (512 reputation)

Group: General Forum Members
Points: 512 Visits: 1163
In your first scripts SELECT statement, I dont see any logic or filters in the WHERE condition where you're telling it to exclude the old data and select new data.

I think what you could also do is build a temporary table, transfer new data into that using a datetime column and just do a simple SELECT against that. The SQL Agent job will not cause replication because this is purely SQL-code related issue.

Thanks,
S

--
Hehe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search