Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML: Adjust Element Positioning


XML: Adjust Element Positioning

Author
Message
cs2data
cs2data
SSC Veteran
SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)

Group: General Forum Members
Points: 232 Visits: 213
I'm having trouble formatting an XML field in a query return. I've boiled the problem part down to this bare logic:


DECLARE @t TABLE(
CoveredEntity varchar(10),
CoverageLoser varchar(10)
)

INSERT INTO @t
--( CoveredEntity, CoverageLoser )
SELECT 'Manager_A', 'Manager_Y'
UNION
SELECT 'Manager_A', 'Manager_Z'
UNION
SELECT 'Manager_B', 'Manager_Y'
UNION
SELECT 'Manager_B', 'Manager_Z'
UNION
SELECT 'Manager_C', 'Manager_Y'
UNION
SELECT 'Manager_C', 'Manager_X'



Now when I do this,

SELECT CoveredEntity AS '@Name',
CoverageLoser AS 'Loser'
FROM @t
FOR XML PATH('Entity'), ROOT('UnClaimEvent')



I get the correct data:

<UnClaimEvent>
<Entity Name="Manager_A">
<Loser>Manager_Y</Loser>
</Entity>
<Entity Name="Manager_A">
<Loser>Manager_Z</Loser>
</Entity>
<Entity Name="Manager_B">
<Loser>Manager_Y</Loser>
</Entity>
<Entity Name="Manager_B">
<Loser>Manager_Z</Loser>
</Entity>
<Entity Name="Manager_C">
<Loser>Manager_Y</Loser>
</Entity>
<Entity Name="Manager_C">
<Loser>Manager_X</Loser>
</Entity>
</UnClaimEvent>



But the .NET team wants it returned such that the Loser elements are nested in a single Entity tag per Entity (identified by the Name attribute):


<UnClaimEvent>
<Entity Name="Manager_A">
<Loser>Manager_Y</Loser>
<Loser>Manager_Z</Loser>
</Entity>
<Entity Name="Manager_B">
<Loser>Manager_Y</Loser>
<Loser>Manager_Z</Loser>
</Entity>
<Entity Name="Manager_C">
<Loser>Manager_Y</Loser>
<Loser>Manager_X</Loser>
</Entity>
</UnClaimEvent>



Any ideas how to accomplish this? I tried an example using multiple CTEs (effectively a self-join) but all I did was end up creating extra nesting. Thanks.
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 1777
Hi, One way of approaching it could be to use a CTE + sub query similar to this:


;WITH xCTE (CoveredEntity) AS
(
SELECT DISTINCT CoveredEntity
FROM @t
)
SELECT CoveredEntity AS '@Name',
(
SELECT CoverageLoser AS 'Loser'
FROM @t t
WHERE t.CoveredEntity = xCTE.CoveredEntity
FOR XML PATH(''), TYPE
)
FROM xCTE
FOR XML PATH('Entity'), ROOT('UnClaimEvent')



cs2data
cs2data
SSC Veteran
SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)SSC Veteran (232 reputation)

Group: General Forum Members
Points: 232 Visits: 213
Perfect. Thanks very much for your quick assistance!
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