Sqlserver
 SQL >> Datenbank >  >> RDS >> Sqlserver

Mit SQL FOR XML eine HTML-Tabelle erzeugen

Die Tabellenzeilen lassen sich direkt so aufbauen:

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') 

Wenn Sie zusätzlich eine Kopfzeile voranstellen möchten, genügt ein 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') 

Die äußere for xml path('tr')-Klausel erzeugt die Zeilen, die inneren Unterabfragen die Zellen.