在SQL中将行转换为XML节点

时间:2021-08-17 23:41:24

I have to return XML from the two column table like the below format

我必须从两个列表中返回XML,如下面的格式

TABLE

month       count
-----       ----
January     578
February    300
March       147
April       45
May         8

XML

XML

<January>578</January>
<February>300</February>
<March>147</March>
<April>45</April>
<May>8</May>

I have tried the below SQL statement,

我试过下面的SQL语句,

SELECT * FROM #temp;

SELECT (
   SELECT monthId AS 'month/@atr', count AS month
   FROM #temp
   FOR XML PATH(''), TYPE
) 
FOR XML PATH('')

And I know the above script is for getting the first column value as attribute. In my case, I need first column value as node and second one as its value.

我知道上面的脚本用于获取第一列值作为属性。在我的例子中,我需要第一列值作为节点,第二列值作为其值。

Thanks for help.

感谢帮助。

3 个解决方案

#1


4  

DECLARE @t TABLE ([month] VARCHAR(50) PRIMARY KEY, [count] INT)

INSERT INTO @t
VALUES
    ('January', 578), ('February', 300),
    ('March', 147), ('April', 45), ('May', 8)

SELECT *
FROM @t
PIVOT (
    SUM(count)
    FOR month IN ([January], [February], [March], [April], [May], [June], [Jule]) 
) p
FOR XML PATH('')

#2


4  

its odd, but serve the purpose,

奇怪,但服务的目的,

DECLARE @MyTable TABLE ( Month VARCHAR(20), Count INT)

INSERT INTO @MyTable (Month, Count) 
VALUES 
     ('January' , 578)
    ,('February', 300)
    ,('March'   , 147)
    ,('April'   , 45 )
    ,('May'     , 8  )

SELECT 
  CAST('<' + Month + '>' + CAST(Count AS VARCHAR(20)) + '</' + Month + '>' AS XML) 
FROM @MyTable
FOR XML PATH('')

---- Output ----

----输出----

<January>578</January>
<February>300</February>
<March>147</March>
<April>45</April>
<May>8</May>

#3


4  

On a direct way you can (maybe) use pivot or you use dynamic SQL:

在直接的方式,您可以(可能)使用数据透视或您使用动态SQL:

DECLARE @tbl TABLE([month] VARCHAR(100),[count] INT);
INSERT INTO @tbl VALUES
 ('January',578)
,('February',300)
,('March',147)
,('April',45)
,('May',8);

DECLARE @cmd VARCHAR(MAX)=
'SELECT ' +
(
    STUFF(
    (
        SELECT ',' + CAST(tbl.[count] AS VARCHAR(100)) + ' AS [' + tbl.[month] + ']'
        FROM @tbl AS tbl
        FOR XML PATH('')
    ),1,1,''
    ) 
)
+
' FOR XML PATH('''');';

EXEC(@cmd);

The result

结果

<January>578</January>
<February>300</February>
<March>147</March>
<April>45</April>
<May>8</May>

But I would not do this...

It was much better (much easier to query!) to create a structure like this:

创建像这样的结构要好得多(更容易查询!)

SELECT tbl.[month] AS [@name]
      ,tbl.[count] AS [*]
FROM @tbl AS tbl
FOR XML PATH('month');

The result

结果

<month name="January">578</month>
<month name="February">300</month>
<month name="March">147</month>
<month name="April">45</month>
<month name="May">8</month>

#1


4  

DECLARE @t TABLE ([month] VARCHAR(50) PRIMARY KEY, [count] INT)

INSERT INTO @t
VALUES
    ('January', 578), ('February', 300),
    ('March', 147), ('April', 45), ('May', 8)

SELECT *
FROM @t
PIVOT (
    SUM(count)
    FOR month IN ([January], [February], [March], [April], [May], [June], [Jule]) 
) p
FOR XML PATH('')

#2


4  

its odd, but serve the purpose,

奇怪,但服务的目的,

DECLARE @MyTable TABLE ( Month VARCHAR(20), Count INT)

INSERT INTO @MyTable (Month, Count) 
VALUES 
     ('January' , 578)
    ,('February', 300)
    ,('March'   , 147)
    ,('April'   , 45 )
    ,('May'     , 8  )

SELECT 
  CAST('<' + Month + '>' + CAST(Count AS VARCHAR(20)) + '</' + Month + '>' AS XML) 
FROM @MyTable
FOR XML PATH('')

---- Output ----

----输出----

<January>578</January>
<February>300</February>
<March>147</March>
<April>45</April>
<May>8</May>

#3


4  

On a direct way you can (maybe) use pivot or you use dynamic SQL:

在直接的方式,您可以(可能)使用数据透视或您使用动态SQL:

DECLARE @tbl TABLE([month] VARCHAR(100),[count] INT);
INSERT INTO @tbl VALUES
 ('January',578)
,('February',300)
,('March',147)
,('April',45)
,('May',8);

DECLARE @cmd VARCHAR(MAX)=
'SELECT ' +
(
    STUFF(
    (
        SELECT ',' + CAST(tbl.[count] AS VARCHAR(100)) + ' AS [' + tbl.[month] + ']'
        FROM @tbl AS tbl
        FOR XML PATH('')
    ),1,1,''
    ) 
)
+
' FOR XML PATH('''');';

EXEC(@cmd);

The result

结果

<January>578</January>
<February>300</February>
<March>147</March>
<April>45</April>
<May>8</May>

But I would not do this...

It was much better (much easier to query!) to create a structure like this:

创建像这样的结构要好得多(更容易查询!)

SELECT tbl.[month] AS [@name]
      ,tbl.[count] AS [*]
FROM @tbl AS tbl
FOR XML PATH('month');

The result

结果

<month name="January">578</month>
<month name="February">300</month>
<month name="March">147</month>
<month name="April">45</month>
<month name="May">8</month>