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?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • 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.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

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

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