Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unlimited emails from a cursor

Status
Not open for further replies.

Wyldcard9

Programmer
Feb 5, 2004
82
US
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?
 
Where's the next fetch?
-Karl

[red] Cursors, Triggers and User Definded Functions are part of the Axis of Evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
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

FETCH NEXT FROM the_cursor into @EmailAddress, @Devid

END
CLOSE the_cursor
DEALLOCATE the_cursor
GO
 
Okay I feel dumb now. Thanks a lot. Too deep, and not seeing the forest from the trees.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top