title: How to send an email from a DB Mail profile ONLY if results are returned from a query
body: <p>I am fairly new to SQL so hoping this will be an easy question. I have been tasked with setting up monitoring and alerting of a specific table that logs all transactions with another server. Using SSMS and the sp_send_dbmail sproc, I want to setup a database mail profile and a SQL Agent Job that runs a query every day to see if there are any transactions that are not successful.</p>
  <p>The query is a stored procedure and I have configured it to only return results if there are failed transactions. Is there a way to ONLY send an email if the query returns results? I just think it would be kind of pointless to send an email if there are no failures.</p>
  <p>I would imagine my job would look something like this, but I'm not sure how to specify the email to only be sent if the query returns results:</p>
  <pre><code>EXEC msdb.dbo.sp_send_dbmail  
      @profile_name = 'myProfile',  
      @recipients = 'testemail@gmail.com',  
      @body = 'Query Results',  
      @subject = 'Daily results' ,    
      @query = '&lt;stored procedure goes here&gt;',
          @attach_query_result_as_file = 1
  </code></pre>
  <p>Thanks for any help you can provide</p>