I'm creating a HL7 Continuity of Care Document (CCD) using FOR XML statements in SQL Server 2008 R2.
I've done A LOT with this method, but this is the first time I have to represent part of the data in a HTML table, which is giving me trouble.
So, I have the following information in a table:
Problem | Onset | Status
---------------------------------
Ulcer | 01/01/2008 | Active
Edema | 02/02/2005 | Active
and I'm trying to render the following
<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>
I'm using this query:
SELECT p.ProblemType AS "td"
, p.Onset AS "td"
, p.DiagnosisStatus AS "td"
FROM tblProblemList p
WHERE p.PatientUnitNumber = @PatientUnitNumber
FOR XML PATH('tr')
And I keep getting the following:
<tr>
<td>Ulcer2008-01-01Active</td>
</tr>
<tr>
<td>Edema2005-02-02Active</td>
</tr>
Anyone got any advice?
Mikael's answer works but so will this:
Rather than using FOR XML PATH('tr'), use FOR XML RAW('tr'), ELEMENTS. This will prevent the values from being concatenated and give you very clean output. Your query would look like this:
I prefer to append the header row using pure markup so I can have a little better control over what is happening. The full code block would look something like this:
EDIT
I wanted to add some extra value that I came up based on the need to format the output table.
The "AS td" alias will produce
<td>value</td>
elements in the markup but not because it understands that a table cell is a td. This disconnect allows us to create fake HTML elements that can be later updated after the query has been executed. For instance, if I wanted to the ProblemType value to be center aligned I can tweak the element name to allow for this. I can't add a style or class to the element name because it breaks alias naming conventions in SQL, but I can create a new element name such as tdc. This will produce<tdc>value</tdc>
elements. While this is not valid markup in any way, it is easy for a replace statement to handle.This will create cell elements with the format
<td class="center">value</td>
. A quick block at the top of the string and you'll have center aligned values with a simple tweak.Another situation I needed to solve was inclusion of links in the markup. As long as the value in the cell is the value you need in the href this is pretty easy to solve. I'll expand the example to include an ID field that I want linked to a detail URL.
This example doesn't account for using the value in the cell inside of the link text but that is a solvable problem with some CHARINDEX work.
My final implementation of this system was for sending HTML emails based on SQL queries. I had a repeated need for cell alignment and common link types so I moved the replace functions into a shared scalar function in SQL so I didn't have to have them in all my stored procedures that sent email.
I hope this adds some value.
i prefer do this: