html - 使用 SQL FOR XML 创建 HTML 表

标签 html sql-server xml tsql html-table

我正在 SQL Server 2008 R2 中使用 FOR XML 语句创建 HL7 护理连续性文档 (CCD)。

我已经用这种方法做了很多,但这是我第一次必须在 HTML 表格中表示部分数据,这给我带来了麻烦。

所以,我在表格中有以下信息:

  Problem  |   Onset    | Status
---------------------------------
  Ulcer    | 01/01/2008 | Active
  Edema    | 02/02/2005 | Active

我正在尝试呈现以下内容

<tr>
    <th>Problem</th>
    <th>Onset</th>
    <th>Status</th>
</tr>
<tr>
    <td>Ulcer</td>
    <td>01/01/2008</td>
    <td>Active</td>
</tr>
<tr>
    <td>Edema</td>
    <td>02/02/2005</td>
    <td>Active</td>
</tr>

我正在使用这个查询:

SELECT    p.ProblemType AS "td"
    , p.Onset AS "td"
    , p.DiagnosisStatus AS "td"
FROM tblProblemList p
WHERE p.PatientUnitNumber = @PatientUnitNumber
FOR XML PATH('tr')

我不断收到以下信息:

<tr>
  <td>Ulcer2008-01-01Active</td>
</tr>
<tr>
  <td>Edema2005-02-02Active</td>
</tr>

有人有什么建议吗?

最佳答案

select 
  (select p.ProblemType     as 'td' for xml path(''), type),
  (select p.Onset           as 'td' for xml path(''), type),
  (select p.DiagnosisStatus as 'td' for xml path(''), type)
from tblProblemList p
where p.PatientUnitNumber = @PatientUnitNumber
for xml path('tr')

要同时添加 header ,您可以使用union all

select 
  (select 'Problem' as th for xml path(''), type),
  (select 'Onset'   as th for xml path(''), type),
  (select 'Status'  as th for xml path(''), type)
union all         
select 
  (select p.ProblemType     as 'td' for xml path(''), type),
  (select p.Onset           as 'td' for xml path(''), type),
  (select p.DiagnosisStatus as 'td' for xml path(''), type)
from tblProblemList p
where p.PatientUnitNumber = @PatientUnitNumber
for xml path('tr')

关于html - 使用 SQL FOR XML 创建 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7086393/

相关文章:

html - 如何将用作表单提交按钮的 div 居中

SQL SERVER 'Contains' 没有返回实际结果

sql-server - sqloledb 是否真的在 Windows Server 2019 上使用 MSOLEDBSQL

python - 使用python从xml文件中删除两个字符串之间的所有行

javascript - 播放/暂停按钮 HTML5 音频

javascript - 我想在网站上快速加载多张图片,最好的方法是什么?

sql-server - 在 Azure Data Studio 中尝试 SQL Notebooks 但在一个单元格中声明的变量未携带到新单元格

java - 我的操作栏没有出现

PHP:对 XML 的 AJAX 请求

javascript - 在 Javascript 中调度大量额外事件的效率如何?