That should do it. Please note that you do not need to manually add tags <tr>and </tr>to your html line. This data is provided to you as part of for xml path('tr'). You probably would like to add </table>at the end.
declare @html varchar(max)
set @html = '<table cellpadding=0 cellspacing=0 border=0>'
set @html +=
cast(
(select
'Column1' as td, '',
'Column2' as td, '',
'right' as [td/@align], 'Column3' as td, ''
for xml path('tr')) as varchar(max)
)
set @html += '</table>'
select @html
Output:
<table cellpadding=0 cellspacing=0 border=0><tr><td>Column1</td><td>Column2</td><td align="right">Column3</td></tr></table>
source
share