Create HTML Table with SQL FOR XML

前端 未结 8 1328
夕颜
夕颜 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:36

    Try this:

    FOR XML raw, elements, root('tr')
    
    0 讨论(0)
  • 2020-11-22 08:43

    i prefer do this:

    select 
    convert(xml,
    (
        select 'column1' as th,
               'column2' as th
        for xml raw('tr'),elements
    )),     
    convert(xml,
    (
        select t1.column1 as td,
               t1.column2 as td
        from #t t1
        for xml raw('tr'),elements
    ))
    for xml raw('table'),elements
    
    0 讨论(0)
  • 2020-11-22 08:47
    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')
    

    To add the header as well you can use 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')
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-22 08:51

    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.

    BEGIN
      SET NOCOUNT ON;
      DECLARE @htmlOpenTable VARCHAR(200) = 
         '<table style="border-collapse: collapse; border: 1px solid #2c3e50; background-color: #f9fbfc;">'
      DECLARE @htmlCloseTable VARCHAR(200) = 
         '</table>'
      DECLARE @htmlTdTr VARCHAR(max) = (        
        SELECT 
           'border-top: 1px solid #2c3e50' as [td/@style], someColumn as td, '',
           'border-top: 1px solid #2c3e50' as [td/@style], someColumn as td, ''
        FROM someTable
        WHERE someCondition
        FOR XML PATH('tr')
      )
      SELECT @htmlOpenTable + @htmlTdTr + @htmlCloseTable
    END
    

    Where someColumn is your attribute from your table

    And someTable is your table name

    And someCondition is optional if you are using WHERE claus

    Please 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

    0 讨论(0)
  • 2020-11-22 08:53

    I ran into this problem awhile ago. Here is how I solved it:

    SELECT
    p.ProblemType AS "td"
    , '' AS "text()"
    , p.Onset AS "td"
    , '' AS "text()"
    , p.DiagnosisStatus AS "td"
    
    FROM tblProblemList p
    WHERE p.PatientUnitNumber = @PatientUnitNumber
    FOR XML PATH('tr')
    
    0 讨论(0)
提交回复
热议问题