If Query Returns results send a email

  • Hi,

    I'm trying to send returned results of a query only if something is returned back in the query through the sp_send_dbmail function..

    I've tried to use the following but it doesn't seem to be working.....

    IF EXISTS (SELECT [QUERY HERE])

    BEGIN

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'Adventure Works Administrator',

    @recipients = 'yourfriend@Adventure-Works.com',

    @subject = 'Test',

    @attach_query_result_as_file = 0 ;

    end

    any ideas ?

  • Suth (10/31/2016)


    Hi,

    I'm trying to send returned results of a query only if something is returned back in the query through the sp_send_dbmail function..

    I've tried to use the following but it doesn't seem to be working.....

    IF EXISTS (SELECT [QUERY HERE])

    BEGIN

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'Adventure Works Administrator',

    @recipients = 'yourfriend@Adventure-Works.com',

    @subject = 'Test',

    @attach_query_result_as_file = 0 ;

    end

    any ideas ?

    'Not working'. Is that all of the information you are prepared to give us?

    Is there an error message?

    Does the statement work if you issue it unconditionally?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • You'll also need to use parameter "@query = " when calling the sp_send_dbmail procedure. Look in BOL for a complete list of parameters.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Question now is, what the hell happened to my post which included code to do this (or maybe this is a duplicate post)? I'll see if I saved the example somewhere and try to repost it tonight.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Deleted.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

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

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