Formating color in SQL Server using FOR XML PATH

前端 未结 3 1279
时光说笑
时光说笑 2021-01-24 23:48

I already used this method, but now I have to use some colors depending on the values. So, I have the following information in a table:

Material | Q1 | Q2
------         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-24 23:54

    With the function mentioned in my comments you'd go like this:

    This is the function

    CREATE FUNCTION dbo.CreateHTMLTable
    (
        @SelectForXmlPathRowElementsXsinil XML
       ,@tblClass VARCHAR(100) --NULL to omit this class
       ,@thClass VARCHAR(100)  --same
       ,@tbClass VARCHAR(100)  --same
    )
    RETURNS XML
    AS
    BEGIN
    
    RETURN 
    (
        SELECT @tblClass AS [@class]  
        ,@thClass AS [thead/@class]
        ,@SelectForXmlPathRowElementsXsinil.query(
                  N'let $first:=/row[1]
                    return 
                     
                    {
                    for $th in $first/*
                    return {if(not(empty($th/@caption))) then xs:string($th/@caption) else local-name($th)}
                    }
                    ') AS thead
        ,@tbClass AS [tbody/@class]
        ,@SelectForXmlPathRowElementsXsinil.query(
                   N'for $tr in /row
                     return 
                     {$tr/@class}
                     {
                     for $td in $tr/*
                     return
                     if(empty($td/@link)) 
                     then {$td/@class}{string($td)}
                     else {$td/@class}{string($td)}
                     }
                     ') AS tbody
        FOR XML PATH('table'),TYPE
    ) 
    END
    GO
    

    --Your test table

    CREATE TABLE #tempo
    (
        q1 INT, q2 INT, name VARCHAR(10)
    );
    
    INSERT INTO #tempo (q1, q2, name)
    VALUES (10, 5, 'low'), (10, 10, 'same'), (10, 20, 'high');
    GO
    

    --Inline CSS for easy formatting

    DECLARE @inlineCSS XML=
    N'';
    

    --This is the actual query

    SELECT @inlineCSS 
          ,dbo.CreateHTMLTable
            (
                 (
                 SELECT 
                   CASE WHEN ISNULL(q1,0)>ISNULL(q2,0) THEN 'low' 
                        ELSE CASE WHEN ISNULL(q2,0)>ISNULL(q1,0) THEN 'high' 
                             ELSE 'same' 
                             END 
                        END AS [@class] 
                  ,name AS Material
                  ,ISNULL(q1,0) AS [Q1]
                  ,ISNULL(q2,0) AS [Q2]
                 FROM #tempo 
                 FOR XML PATH('row'),ELEMENTS XSINIL),NULL,NULL,NULL
            )
    FOR XML PATH('body'),ROOT('html');
    

    --Hint: Using classnames instead of the three ,NULL,NULL,NULL allows to place general CSS classes to the

    , the and the .

    --Clean-Up

    GO
    DROP TABLE #tempo
    GO
    DROP FUNCTION dbo.CreateHTMLTable;
    

    This is the result (click "Run" to see the output)

    
      
        
        
    Material Q1 Q2
    low 10 5
    same 10 10
    high 10 20

提交回复
热议问题