Avoiding a Cursor/Loop

  • Hi have a table that I want to put the data of multiple columns into a @Body variable so it can be emailed. I'm really unsure of how to do this without a loop or cursor.

    The table will contain data like so:

    InvoiceNo varchar Message varchar

    12345 Failed to Export

    56978 Issue with Customer

    The @Body should look something like this.

    There were issues processing these invoices:

    12345 - Failed To Export

    56978 - Issue with Customer

    I would then execute a CDOSend to email the "report" to the appropriate person.

    I know this can be done with a cursor that's easy... but how if at all could it be done without a Cursor/Loop?

  • DECLARE @Body NVARCHAR(2000)

    SELECT

    @Body = @Body + InvoiceNo + ' - ' + Message + CHAR(13) + CHAR(10)

    FROM ....

    WHERE ...restriction for one contact.

    This should work for one contact at a time.

    ---------------------------------------------
    [font="Verdana"]Nothing is impossible.
    It is just a matter of time and money.[/font]

  • Yeah but I want to get messages for multiple invoices in one email.... so I would still have to do a loop or a cursor with your code.

  • No. I don't think so.

    How do you know which invoices to use? Use the same logic in the WHERE clause.

    ---------------------------------------------
    [font="Verdana"]Nothing is impossible.
    It is just a matter of time and money.[/font]

  • I missed the @Body = @Body + part....

    That does work. Thanks for the help.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply