Hello I am using TSQL and an HTML table to run a query and send the results via an email. I cannot preserve the line breaks in the result set.
I have some test code below. When I run the query via management studio I get the results;
line
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line 1
Line 2
Line 3
But the email output shows;
Line 1 Line 2 Line 3
Can I preserve the line breaks in sp_sendmail?
code below -
/////////////////////////////////////////////////////
create table test (line nvarchar(max))
go
declare @testtext nvarchar(max)
set @testtext = 'Line 1' + char (10)+ CHAR(13) + 'Line 2' + char (10)+ CHAR(13) + 'Line 3'
select @testtext
insert into test select @testtext
declare @tableHTML nvarchar(max)
SET @tableHTML =
N'<H1>TEST</H1>' +
N'<tr>
<th>test</th>'+
'</tr>'
+
CAST ( (select
td =t.line, ' '
from
test t
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'me@myemailaddress',
@subject = 'test',
@body_format = 'HTML' ,
@body = @tableHTML ;
go
select * from test
drop table test
///////////////////////////////////////////////////