Cómo agregar un atributo a centrado en elementos consulta FOR XML PATH
-
04-10-2019 - |
Pregunta
Estoy construyendo algo de HTML que se incluirán en el cuerpo de un correo electrónico y envía utilizando sp_send_dbmail. Me gustaría alineación derecha algunas de las columnas. ¿Hay una manera fácil de hacer esto (por debajo de reescribirlo utilizando FOR XML EXPLICIT)?
declare @html varchar(max)
set @html = '<table cellpadding=0 cellspacing=0 border=0>'
set @html +=
cast(
(select
'Column1' as td, '',
'Column2' as td, '',
'Column3' as [td align=right] /* Would like to do something like this */
for xml path('tr')) as varchar(max)
)
set @html += '</table>'
Solución
Esto debería hacerlo. Tenga en cuenta que no es necesario añadir manualmente las etiquetas <tr>
y </tr>
a la cadena html. Los que se dan a usted como parte de la for xml path('tr')
. Es probable que significan para añadir </table>
hasta el final en su lugar.
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
La salida es:
<table cellpadding=0 cellspacing=0 border=0><tr><td>Column1</td><td>Column2</td><td align="right">Column3</td></tr></table>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow