Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CDONTS and WHILE (@@FETCH_STATUS)

Posted on 2004-10-13
2
Medium Priority
?
509 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 375 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

705 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