title: SQL Query to send e-mail based on records number
body: <p>I have this sql query:</p>
  
  <pre><code>DECLARE @tableHTML  NVARCHAR(MAX) ;
  
  SET @tableHTML =
  N'&lt;H1&gt;Table name&lt;/H1&gt;' +
  N'&lt;table border="1"&gt;' +
  N'&lt;tr&gt;&lt;th&gt;Column name1&lt;/th&gt;&lt;th&gt;Column name2&lt;/th&gt;' +
  N'&lt;th&gt;Column name3&lt;/th&gt;&lt;th&gt;Column name4&lt;/th&gt;&lt;/tr&gt;' +
  CAST ( ( select td = login_time, '', 
                  td = last_batch, '', 
                  td = hostname ,'' ,
                  td = loginame, ''
      from master.dbo.sysprocesses
      where loginame&lt;&gt;'sa' and program_name=''
      order by last_batch Desc
            FOR XML PATH('tr'), TYPE 
  ) AS NVARCHAR(MAX) ) +
  N'&lt;/table&gt;' ;
  
  EXEC msdb.dbo.sp_send_dbmail 
  @profile_name = 'profile name',
  @recipients='e-mail account',
  @subject = 'e-mail subject',
  @body = @tableHTML,
  @body_format = 'HTML' ;
  </code></pre>
  
  <p>But I need only send the e-mail if there are records on query.</p>
  
  <p>Can someone help???</p>
  
  <p>Thank you.</p>