Solved

CDONTS and WHILE (@@FETCH_STATUS)

Posted on 2004-10-13
2
505 Views
Last Modified: 2010-05-18
Hi,

I am trying to loop through a database and send an email out when certain constraints are met, I have used print to see the results which in this case correctly pulls out 4 records, however it only sends 1 email, and ideas on what I am doing wrong?


DECLARE @number varchar(100)
DECLARE @msg varchar(255)
DECLARE fetch_cursor cursor for

SELECT fault_id
FROM report_table
WHERE status ='O' and close_date is null and datediff("d", open_date, getdate())>1
OPEN fetch_cursor

DECLARE @Address varchar(255), @Message varchar(8000),
@Subject varchar(255), @From varchar(255), @CDO int, @OLEResult int, @Out int

EXECUTE @OLEResult = master.dbo.sp_OACreate 'CDONTS.NewMail', @CDO OUT
FETCH NEXT FROM fetch_cursor into @number
WHILE (@@FETCH_STATUS <> -1)
BEGIN
IF (@@FETCH_STATUS <> -2)
BEGIN
      Set @Address = 'peter@whoba.co.uk'
      Set @Message = 'Fault ID (' + @number + ') is still open.'
      Set @Subject  = 'Fault Log Update'
      Set @From = 'peter@whoba.co.uk'
      
IF @OLEResult <> 0

      PRINT 'CDONTS.NewMail'
        PRINT 'ItemNo = ' + @number
        execute @OLEResult = master.dbo.sp_OAMethod @CDO, 'Send', Null, @From, @Address, @Subject, @Message, 0

        IF @OLEResult <> 0 PRINT 'Send'
          PRINT ' '
      END

FETCH NEXT FROM fetch_Cursor INTO @number
END
EXECUTE @OLEResult = master.dbo.sp_OADestroy @CDO
 
CLOSE fetch_Cursor
DEALLOCATE fetch_Cursor


=========== PRINT RESULTS====================

ItemNo = SB7736
 
ItemNo = SB7748
Send
 
CDONTS.NewMail
ItemNo = SB7735
Send
 
CDONTS.NewMail
ItemNo = SB7764
Send
 
===================================

Thanks for any help








0
Comment
Question by:trojan_uk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
SQL_Stu earned 125 total points
ID: 12296561
Have you tried moving this line into your cursor loop?

EXECUTE @OLEResult = master.dbo.sp_OACreate 'CDONTS.NewMail', @CDO OUT
0
 

Author Comment

by:trojan_uk
ID: 12296604
Thanks,

Funny enough I had just tried that before your messge came in:

FETCH NEXT FROM fetch_cursor into @number
WHILE (@@FETCH_STATUS <> -1)
BEGIN      
EXECUTE @OLEResult = master.dbo.sp_OACreate 'CDONTS.NewMail', @CDO OUT
IF (@@FETCH_STATUS <> -2)
BEGIN


And it worked, however had I been a few minutes slower you would have given me the right answer.

Thanks again
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question