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?
All these answers work fine but I ran into a problem recently where I wanted to have conditional formatting on the html ie. I wanted the style property of the td to vary based on data. The basic format is similar with the addition of setting td = :
To add in conditional formatting to this you simply need to add a case statement:
There are a tremendous answers already. I just wanted to add that you can also use styles within your query which might be a good in terms of design.
Where
someColumn
is your attribute from your tableAnd
someTable
is your table nameAnd
someCondition
is optional if you are usingWHERE
clausPlease note that the query is only selecting two attributes, you can add as many as you want and also you can change on the styles.
Of course you can use styles in other ways. In fact, it is always better to use external CSS, but it is a good practice to know how to put inline styles because you might need them
I ran into this problem awhile ago. Here is how I solved it:
This is a generic solution with a
FUNCTION
onXML
-base using FLWORIt will transform any
SELECT
into a XHTML table.It works (tested) with 2008R2+, but I'm pretty sure this would work on 2008, might be even on 2005, too. If someone wants to verify this, please leave a comment. Thx
The following function replaces all the various functions I provided before (see the previous version if needed)
The easiest call
A mock-up table with some values
--The call must enclose the
SELECT ... FOR XML
in paranthesis!--click run snippet to see the result!
If you need headers with blanks
If your table contains a column with a blank in its name, or if you want to set a column's caption manually (multi langugage support!), or if you want to replace a CamelCaseName with an out-written caption, you can pass this as attribute:
Full CSS-support and hyper-links
You can use attributes to pass over a link or a row-based and even a value-based class to mark columns and even cells for CSS styling.
As a possible enhancement one might pass in a one-row-footer with aggregated values as additional parameter and append it as
<tfoot>
To add the header as well you can use
union all
.Try this: