Pregunta

I am using SQL Server 2008 and am trying to send emails to customers about their overdue ADV returns. There is a 30 day, 45 day and 50 day letter. I have noticed this issue currently on the 30 day and 45 day letters.

I have a query that pulls the right people. For instance on the 30 day letter today it pulled 4 records and they are correct. for instance the data looks like

john   item1
david  item1
david  item2
fred   item1

So what is happening is I get 4 emails, but john is missing and david gets 3 emails one of which is a duplicate.

Here is the code for sending the emails

OPEN C1

FETCH NEXT FROM C1 
INTO @sronum, @transdate,@item,@desc, @days, @email, @NAME

WHILE @@FETCH_STATUS = 0
BEGIN
    FETCH NEXT FROM C1 
    INTO @sronum, @transdate,@item,@desc,@days, @email, @NAME

    set @email = 'internal test email address'
    set @subject = 'ADV Return is 30 days past due'
    set @body = 'Dear ' + @name + ', </br></br>'  + more text

    EXEC msdb.dbo.sp_send_dbmail
         @recipients = @email
         ,@Body = @Body
         ,@subject = @Subject
         ,@body_format = 'html'
         ,@exclude_query_output = 1
END

CLOSE C1
DEALLOCATE C1
¿Fue útil?

Solución

A bit commented;

OPEN C1

-- Fetch first result and throw it away since next fetch will be done
-- before using the values
FETCH NEXT FROM C1 INTO
     @sronum, @transdate,@item,@desc, @days, @email, @NAME

-- If the fetch went well, loop
WHILE @@FETCH_STATUS = 0
BEGIN

 -- Fetch the next row and ignore if it went well for now
 FETCH NEXT FROM C1 INTO
     @sronum, @transdate,@item,@desc,@days, @email, @NAME
 set @email = 'internal test email address'
 set @subject = 'ADV Return is 30 days past due'
 set @body = 'Dear ' + @name + ', </br></br>'  + more text

 -- Send a mail to the possibly valid address
 EXEC msdb.dbo.sp_send_dbmail
  @recipients = @email
  ,@Body = @Body
  ,@subject = @Subject
  ,@body_format = 'html'
  ,@exclude_query_output = 1

-- Jump back to the start of the loop to check if the mail just sent 
-- was supposed to be sent.
END
CLOSE C1

DEALLOCATE C1

The simplest fix is to move the second fetch after the email has been sent. That will check if the first result was valid, send a mail, fetch a new row and go back to the validity check to possibly send another mail.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top