4

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

TABLE

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

XML

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

I have tried the below SQL statement,

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.

1
  • Is it possible by SQL query itself? Commented Jul 25, 2016 at 10:15

3 Answers 3

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('')
Sign up to request clarification or add additional context in comments.

1 Comment

As the month's names are a closed set, PIVOT is a good approach... +1 from my side.
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>

1 Comment

I would (almost) never advise to build XML with string methods, but in this case it is a very creative approach, +1 from my side!
4

On a direct way you can (maybe) use pivot or you use dynamic 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.