Skip to content
Advertisement

Convert a SQL query result table to an HTML table for email

I am running a SQL query that returns a table of results. I want to send the table in an email using dbo.sp_send_dbMail.

Is there a straightforward way within SQL to turn a table into an HTML table? Currently, I’m manually constructing it using COALESCE and putting the results into a varchar that I use as the emailBody.

Is there a better way to do this?

Advertisement

Answer

Here is one way to do it from an article titled “Format query output into an HTML table – the easy way [archive]“. You would need to substitute the details of your own query for the ones in this example, which gets a list of tables and a row count.

Once you have @body, you can then use whatever email mechanism you want.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement