首页 > 解决方案 > 使用 T-SQL 如何将 html 属性添加到 FOR XML PATH

问题描述

我必须在存储过程中使用 T-SQL (SQL Server) 从SELECT. 我不确定是否/如何最好地向基本 html 表属性添加其他属性;

select 
    (select 'ID' as 'th' for xml path(''),  type),
    (select 'StartDate'   as 'th' for xml path(''), type),
    (select 'LPlace'  as 'th' for xml path(''), type)
union all 
select 
    (select p.[Id]     as 'td' for xml path(''), type),
    (select p.[Created]           as 'td' for xml path(''), type),
    (select p.[LPlace] as 'td' for xml path(''), type)
from [SourceTbl] p
for xml path('tr'), ROOT('table')

结果:

<table>
  <tr>
    <th>ID</th>
    <th>StartDate</th>
    <th>LPlace</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2019-10-13T11:36:22.0050221</td>
    <td>K7</td>
  </tr>
  <tr>
    <td>2</td>
    <td>2019-03-10T13:12:07.1008696</td>
    <td>J6</td>
  </tr>

我想补充的是:

<table style=''font-family: Arial;font-size: 8pt;border: solid 1px #888;border-collapse: collapse;

<th style=''border: 1px solid #090F20;background-color: #0D417E;padding: 2px 0;color: #F8F8FF;font-size: 9pt;''>ID</th>

谢谢

标签: sql-servertsqlfor-xml-path

解决方案


推荐阅读