Solved

CDONTS and WHILE (@@FETCH_STATUS)

Posted on 2004-10-13
2
485 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

803 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