1

I have a query that returns data as XML using the "for XML Clause" and then sends the result as the body of an email like so.

Declare @messBody as nvarchar(max)
Set @messBody = (Select * from tablehere where state = 1 for xml Auto)
Begin
  Exec msdb.dbo.ap_send_dbmail
      @profile_name = 'ProfileNameHere'
      @recipients = '[email protected]'
      @body = '@messBody
      @subject = 'SubjectHere'
End

It works and I get the email with the info I need but the body of the email is ugly XML. I would like to be able to transform it into pretty HTML. Can this be done in SQL Server?

3 Answers 3

2

I think you would need to use the CLR for that to do it by applying an XSLT transform.

Sign up to request clarification or add additional context in comments.

2 Comments

@spinon. Yep. "apply xslt sql server" came up trumps for me :-)
Yeah sometimes I wonder if people even search for a minute before they post things. So many answers if I don't know off the top of my head I can find in less than a minute of searching on Google.
1

One way would be to write a clr proc. Here is an article showing how: Link

1 Comment

The link is gone. This might help, c-sharpcorner.com/article/….
0
SELECT 
Col1 as 'td', '', 
Col2 as 'td', '', 
Col3 as 'td' 
FROM SomeTable FOR XML PATH('tr'), ELEMENTS

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/05404640-6eed-458e-9514-80ff8bd002f8/sending-mail-with-html-format-in-send-mail-task

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.