Solved

CDONTS and WHILE (@@FETCH_STATUS)

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL 2014 get SPIDs of users 6 26
SQL server 2008 SP4 29 34
SQL query to summarize items per month 5 28
Update in Sql 7 0
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now