I am getting unlimited emails from a cursor:
DECLARE @EmailAddress varchar(50)
DECLARE @Devid int
DECLARE the_cursor CURSOR FOR
SELECT EmailAddress, Devid FROM [Kovis_Data].[dbo].[Email_cache_full]
WHERE EmailAddress IS NOT NULL
OPEN the_cursor
FETCH NEXT FROM the_cursor into @EmailAddress, @Devid
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC xp_sendmail @recipients = @EmailAddress,
@subject = 'Cache full notification',
@message = 'One of the Cache devices is full',
@width = 100
END
CLOSE the_cursor
DEALLOCATE the_cursor
GO
I am trying to send an email out when any of the cache's get full. One of the tables gets a flag turned on. I originally tried a SQL Server Agent job with two steps. One step to read the table, and the second one to email the query results. That worked great; with one exception. If the first query returned no results I still got an email. I want to run this nightly, and only get an email if there are results. So I read on another thread on this site to first write to a little table, and then use the cursor to go across it. The table has three records in it:
pbury@wpas-inc.com 2
pbury@wpas-inc.com 16
pbury@wpas-inc.com 18
I would expect three emails. I am having to stop the query, because I am getting hundrends of emails. What did I do wrong?
DECLARE @EmailAddress varchar(50)
DECLARE @Devid int
DECLARE the_cursor CURSOR FOR
SELECT EmailAddress, Devid FROM [Kovis_Data].[dbo].[Email_cache_full]
WHERE EmailAddress IS NOT NULL
OPEN the_cursor
FETCH NEXT FROM the_cursor into @EmailAddress, @Devid
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC xp_sendmail @recipients = @EmailAddress,
@subject = 'Cache full notification',
@message = 'One of the Cache devices is full',
@width = 100
END
CLOSE the_cursor
DEALLOCATE the_cursor
GO
I am trying to send an email out when any of the cache's get full. One of the tables gets a flag turned on. I originally tried a SQL Server Agent job with two steps. One step to read the table, and the second one to email the query results. That worked great; with one exception. If the first query returned no results I still got an email. I want to run this nightly, and only get an email if there are results. So I read on another thread on this site to first write to a little table, and then use the cursor to go across it. The table has three records in it:
pbury@wpas-inc.com 2
pbury@wpas-inc.com 16
pbury@wpas-inc.com 18
I would expect three emails. I am having to stop the query, because I am getting hundrends of emails. What did I do wrong?