Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL email notification Expand / Collapse
Author
Message
Posted Tuesday, November 24, 2009 6:52 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 28, 2014 10:19 PM
Points: 16, Visits: 111
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
Post #824247
Posted Wednesday, November 25, 2009 7:26 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, October 31, 2011 1:10 PM
Points: 480, Visits: 1,163
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


--
Post #824533
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse