How to choose returned column name in a SELECT FOR XML query?

后端 未结 6 795
时光取名叫无心
时光取名叫无心 2021-02-06 20:49

MS SQL has a convenient workaround for concatenating a column value from multiple rows into one value:

SELECT col1
 FROM table1
 WHERE col2 = \'x\'
 ORDER by col         


        
6条回答
  •  死守一世寂寞
    2021-02-06 21:18

    For EXPLICIT xml generation - with unions you need to wrap results one more time (As a bonus result as XML):

    SELECT 
        CAST(  
            (
                SELECT 
                    * 
                FROM (
                    SELECT 
                        1 AS Tag
                        ,NULL AS Parent
                        ...
                    UNION ALL
                    SELECT ...
                    FOR XML EXPLICIT
                )
            ) as XML) as [MyName]
    

提交回复
热议问题