You are here:Home » tsql » Send Email in Table format and HTML in SQL Server

Send Email in Table format and HTML in SQL Server

DECLARE @tableHTML NVARCHAR(MAX) ;
DECLARE @tableDate datetime ;
SET @tableDate = getdate()
SET @tableHTML=
N' HTML Header
' +
N'

HTML Header 1

' +
N'' +
N'' +
CAST ( ( SELECT td = id, '',
td = [Name], ''
from TableName
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'
IdName
'

EXEC msdb.dbo.sp_send_dbmail
@recipients=N'test@test.com',
@body= @tableHTML,
@subject ='xyz',
@body_format = 'HTML',
@profile_name = 'ProfileName'