SQL SERVER生成HTML Table


select 
  (select BraCode as 'td' for xml path(''), type),
  (select BraName as 'td' for xml path(''), type),
  (select BraPrefix as 'td' for xml path(''), type)
from Bra_Mst 
for xml path('tr')

 效果


  HKG
  Hong Kong
  HKG


  SHA
  Shanghai
  SHA

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

转自: https://stackoverflow.com/questions/7086393/create-html-table-with-sql-for-xml/39487565#39487565

SQL