SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Query Help using XML


SQL Query Help using XML

Author
Message
s-sql
s-sql
Old Hand
Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)

Group: General Forum Members
Points: 355 Visits: 492
Looking for SQL Query help on following.

Have a key value pair, as below.


declare @t table (id int, [key] varchar(10), value varchar(10))
insert @t values (1, 'ColA', 'ABC123')
insert @t values (1, 'colB', 'DEF456')
insert @t values (2, 'colA', '2ColABC')
insert @t values (2, 'colC', '2ColDef')
insert @t values (2, 'colE', '2Colxyz')

Need to bundle the Key-value combination into XML for each ID row. The expected results are
1, '<AdditionalCols><Col ColName="colA">ABC123</Col><Col ColName="colB">DEF456</Col></AdditionalCols>'
2, '<AdditionalCols><Col ColName="colA">2ColABC</Col><Col ColName="colC">2ColDef</Col><Col ColName="colE">2Colxyz</Col></AdditionalCols>'

Tried few combinations using FOR XML, but could not get the desired result at each row level.
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3231 Visits: 24128


SELECT a.id,
(SELECT b.[key] AS "@ColName",
b.value AS "text()"
FROM @t b
WHERE b.id=a.id
FOR XML PATH('Col'),ROOT('AdditionalCols'),TYPE) AS keyvalues
FROM @t a
GROUP BY a.id
ORDER BY a.id;



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




s-sql
s-sql
Old Hand
Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)

Group: General Forum Members
Points: 355 Visits: 492
Thank you very much. The solution worked, and this was exactly what I was looking for..!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search