Search code examples
sql-serversql-server-2008-r2sp-send-dbmail

sp_send_dbmail is sending a blank email when the queried table is empty


I am using the stored procedure msdb.dbo.sp_send_dbmail to send an HTML email with a queried table build into the text. I used the following code example from Microsoft's MSDN to create my email sending stored procedure:

DECLARE @tableHTML  NVARCHAR(MAX) ;  

SET @tableHTML =  
    N'<H1>Work Order Report</H1>' +  
    N'<table border="1">' +  
    N'<tr><th>Work Order ID</th><th>Product ID</th>' +  
    N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +  
    N'<th>Expected Revenue</th></tr>' +  
    CAST ( ( SELECT td = wo.WorkOrderID,       '',  
                    td = p.ProductID, '',  
                    td = p.Name, '',  
                    td = wo.OrderQty, '',  
                    td = wo.DueDate, '',  
                    td = (p.ListPrice - p.StandardCost) * wo.OrderQty  
              FROM AdventureWorks.Production.WorkOrder as wo  
              JOIN AdventureWorks.Production.Product AS p  
              ON wo.ProductID = p.ProductID  
              WHERE DueDate > '2004-04-30'  
                AND DATEDIFF(dd, '2004-04-30', DueDate) < 2   
              ORDER BY DueDate ASC,  
                       (p.ListPrice - p.StandardCost) * wo.OrderQty DESC  
              FOR XML PATH('tr'), TYPE   
    ) AS NVARCHAR(MAX) ) +  
    N'</table>' ;  

EXEC msdb.dbo.sp_send_dbmail @recipients='yourfriend@Adventure-Works.com',  
    @subject = 'Work Order List',  
    @body = @tableHTML,  
    @body_format = 'HTML' ;  

The code works and looks perfect when there is data returned from my query. However, if zero rows are returned, my email is sent with a completely empty body (any HTML before and after the table is not shown). Is there any way to have the email send correctly when there is no data in my SQL query?


Solution

  • It's blank cause it's concatenating a NULL / empty set. I.e. select 'this' + null You should be able to wrap that with ISNULL though and force a blank space.

    DECLARE @tableHTML  NVARCHAR(MAX) ;  
    
    SET @tableHTML =  
        N'<H1>Work Order Report</H1>' +  
        N'<table border="1">' +  
        N'<tr><th>Work Order ID</th><th>Product ID</th>' +  
        N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +  
        N'<th>Expected Revenue</th></tr>' +  
        ISNULL(CAST ( ( SELECT td = wo.WorkOrderID,       '',  
                        td = p.ProductID, '',  
                        td = p.Name, '',  
                        td = wo.OrderQty, '',  
                        td = wo.DueDate, '',  
                        td = (p.ListPrice - p.StandardCost) * wo.OrderQty  
                  FROM AdventureWorks.Production.WorkOrder as wo  
                  JOIN AdventureWorks.Production.Product AS p  
                  ON wo.ProductID = p.ProductID  
                  WHERE DueDate > '2004-04-30'  
                    AND DATEDIFF(dd, '2004-04-30', DueDate) < 2   
                  ORDER BY DueDate ASC,  
                           (p.ListPrice - p.StandardCost) * wo.OrderQty DESC  
                  FOR XML PATH('tr'), TYPE   
        ) AS NVARCHAR(MAX) ),'') +  
        N'</table>' ;  
    
    EXEC msdb.dbo.sp_send_dbmail @recipients='yourfriend@Adventure-Works.com',  
        @subject = 'Work Order List',  
        @body = @tableHTML,  
        @body_format = 'HTML' ;