Create HTML Table with SQL FOR XML

前端 未结 8 1422
夕颜
夕颜 2020-11-22 08:34

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

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 08:49

    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 = :

    declare @body nvarchar(max)
    set @body = 
    cast
    (select 
    'color:red' as 'td/@style', td = p.ProblemType, '',
    td = p.Onset, '',
    td = p.DiagnosisStatus, ''
    from tblProblemList p
    where p.PatientUnitNumber = @PatientUnitNumber
    for xml path('tr'), type)
    as nvarchar(max)
    

    To add in conditional formatting to this you simply need to add a case statement:

    declare @body nvarchar(max)
    set @body = 
    cast
    select 
    cast (case 
    when p.ProblemType = 1 then 'color:#ff0000;'
    else 'color:#000;'
    end as nvarchar(30)) as 'td/@style',
    td = p.ProblemType, '',
    td = p.Onset, '',
    td = p.DiagnosisStatus, ''
    from tblProblemList p
    where p.PatientUnitNumber = @PatientUnitNumber
    for xml path('tr'), type)
    as nvarchar(max)
    

提交回复
热议问题