Avatar of Kevin Smith
Kevin SmithFlag for United States of America asked on

How do I show blank cells in html formatted email sent from sql database mail?

I have the following stored procedure that sends an html email, but if there's no data in one field it throws the next column into the first empty column.  How do I correct that?
SET @tableHTML =
    N'<h1>Upcoming QA Dates</h1>'
  + N'<table border="1">'
  + N'<tr><th>Job Number</th>'
  + N'<th>FU Date</th>'
  + N'<th>PM Name</th></tr>'
  + CAST ( (  SELECT
                  td = p.JobNumber    , ''
                , td = wo.FUDate2_60   ,  ''
                , td = pm.PMName   ,  ''
              FROM (projmgmtsrv.dbo.tbl_QA AS wo
              LEFT JOIN projmgmtsrv.dbo.tbl_Jobs AS p
              ON wo.ProjectID = p.ProjectID)
              LEFT JOIN projmgmtsrv.dbo.tbl_PM AS pm ON p.PM = pm.PMID              
              FOR XML PATH('tr'), TYPE
           ) AS NVARCHAR(MAX)
  + N'</table>';
EXEC msdb.dbo.sp_send_dbmail
@profile_name ='mssjobbook'
, @recipients='email@here.com'
, @subject = 'Message Subject'
, @body = @tableHTML
, @body_format = 'HTML';
Microsoft SQL ServerMicrosoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
David Todd

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

... and if that doesn't work, here is something else to try:

  + CAST ( (  SELECT
             td = isnull(p.JobNumber,'&nbsp;')    , ''
           , td = isnull(wo.FUDate2_60,'&nbsp;')   ,  ''
           , td = isnull(pm.PMName,'&nbsp;')   ,  ''

Open in new window

Kevin Smith

those cause it not to email at all...
David Todd


I try to avoid using XML, but isn't the repeated syntax of td = wrong for a select query? Shouldn't those aliases be different?

Just asking ...

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy