November 24, 2009 at 6:52 pm
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@ObjectNumbervarchar (64),
@DescriptionvarChar(4000),
@Titlevarchar (255), -- ObjTitles.Title is not null
@EnteredDatedatetime,
@Departmentvarchar (64) -- Departments.Department is not null
declare@DepartmentIDint
declare @Body1varchar (8000)
declare @Subject1varchar (4000)
declare@Stylevarchar (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
fromObjects 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
selectO.ObjectID
fromObjects 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
whereO.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
November 25, 2009 at 7:26 am
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:
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply