How to send xml data in SQL Server database mail?

Hello, I have a table which has a xml column and two varchar columns, and I would like to send this table as an email report.
I used SQL Server Database mail and scripts similar as below, but the xml column becomes NULL in the email report.   May I ask if someone knows how I can send the email, with both varchar data and xml data? I would like the xml data to show in xml format so that user can click on it for more details..  Thanks in advance :-)

DECLARE @tableHTML  NVARCHAR(MAX) ;
SET @tableHTML =
N'<head>
<Style>
body {font-family: Verdana;}
</style>
</head>'+
 N'<table border="1" width="90%">' +
 N'<tr><th>Duration</th><th>SessionID</th><th>SQL_Text</th></tr>'+
      CAST ( ( SELECT td = Duration,'',
                    td = SessionID, '',
                    td = SQL_Text, ''
  from Table FOR XML PATH('tr'), TYPE  
    ) AS xml ) +     N'</table><br><br>'    ;
      
declare @subject varchar(300)
select @subject='email report'
EXEC msdb.dbo.sp_send_dbmail
@profile_name='DBMailService',
@recipients='email@hotmail.com',
@subject = @subject,
@body = @tableHTML,
@body_format = 'HTML'
GO
Vivian_ShiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
I would like the xml data to show in xml format so that user can click on it for more details
That's not possible, cause it's not supported by the most HTML viewing e-mail clients.

Send the detail data as attachement.
0
Shaun VermaakTechnical Specialist IVCommented:
You need to HTML encode the XML content. Here is an example
CREATE FUNCTION HtmlEncode
(
    @UnEncoded as varchar(500)
)
RETURNS varchar(500)
AS
BEGIN
  DECLARE @Encoded as varchar(500)

  --order is important here. Replace the amp first, then the lt and gt. 
  --otherwise the &lt will become &amp;lt; 
  SELECT @Encoded = 
  Replace(
    Replace(
      Replace(@UnEncoded,'&','&amp;'),
    '<', '&lt;'),
  '>', '&gt;')

  RETURN @Encoded
END
GO

Open in new window

https://stackoverflow.com/questions/639393/html-encoding-in-t-sql
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vivian_ShiAuthor Commented:
Thanks a lot, it works for me perfectly!
0
ste5anSenior DeveloperCommented:
D'oh??

Modern e-Mail clients support HTML5 and UTF-8 as encoding. Thus there is no special action necessary when creating HTML:

DECLARE @Html NVARCHAR(MAX) = N'';
DECLARE @Template NVARCHAR(MAX) = N'
<html>
    <head>
        <meta charset="utf-8" />
        <style type="text/css">
            body { margin: 0; padding: 0; background: white; font-family: Segoe  UI, Verdana, Arial, Sans-Serif; height: 100%; margin-bottom: 1px; text-align: center; }
            table { border-collapse: collapse; font-size: 12px; }
            td, th { margin: 0; padding: 0; text-align: left; vertical-align: top; }
            table { width: 100%; }
            td, th { padding: 4px; }
            caption { padding: 4px; text-align: left; }
            .blue-1 { background: #00748c; color: #ffffff; }
            .blue-3 { background: #70a4b8; color: #ffffff; }
        </style>
    </head>
    <body>
        <table>
            <caption class="blue-1">Session Data</caption>
        <thead>
            <tr class="blue-3"><th>Duration</th><th>Session ID</th><th>SQL Text</th></tr>
        </thead>
            <!-- CONTENT -->
        </table>
    </body>
</html>';

WITH [Data]
AS ( SELECT TOP 5 DATEDIFF(SECOND, DES.login_time, GETDATE()) AS Duration ,
            DES.session_id AS SessionID ,
            DES.status AS SQL_Text
     FROM   sys.dm_exec_sessions DES )
SELECT @Html = REPLACE(@Template ,
                       '<!-- CONTENT -->' ,
                       (   SELECT D.Duration AS [td] ,
                                  NULL ,
                                  D.SessionID AS [td] ,
                                  NULL ,
                                  D.SQL_Text +'<test>' AS [td]
                           FROM   [Data] D
                           FOR XML PATH('tr'), ROOT('tbody')));

SELECT @Html;

Open in new window


Just this
I would like the xml data to show in xml format so that user can click on it for more details..
is not possible.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.