Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have following query
SQL
select r.AccommodationId as 'Property', r.RoomId as 'RoomId' from	BookingEngine_Live.dbo.Rooms r FOR XML RAW ('PropertyDataSet'),ROOT ('Transaction'),ELEMENTS ; 

which gives me below result:

XML
<transaction>
  <propertydataset>
    <property>1</property>
    <roomid>51</property>
  </propertydataset>
  <propertydataset>
    <property>1</property>
    <roomid>53</property>
  </propertydataset>  
  <propertydataset>
    <property>1</property>
    <roomid>65</property>
  </propertydataset>  
  <propertydataset>
    <property>2</property>
    <roomid>51</property>
  </propertydataset>  
  <propertydataset>
    <property>2</property>
    <roomid>481</property>
  </propertydataset>
  <propertydataset>
    <property>2</property>
    <roomid>101739578</property>
  </propertydataset> 
</transaction>

while I need the result as below:

XML
<transaction>
  <propertydataset>
    <property>1</property>
    <roomid>51</property>
    <roomid>53</property>
    <roomid>65</property>
  </propertydataset>  
  <propertydataset>
    <property>2</property>
    <roomid>51</property>
    <roomid>481</property>
    <roomid>101739578</property>
  </propertydataset> 
</transaction>


What I have tried:

In simple words, I dont want property element to be repeated for every RoomID element. Please guide. Thanks.
Posted
Updated 6-Jan-17 7:46am
v2

1 solution

The FOR XML option always translates the resulting table row by row. The SELECT statemtent you're using will result in:

Property           RoomId
-------------------------
1                   51
1                   53
1                   65
2                   51
2                   481
2                   101739578


To get the XML structure as needed you have to create a query that will result in:
Property            RoomId            RoomID      RoomId
--------------------------------------------------------
1                     51                53         65
2                     51               481  101739578

This isn't possible because you can't have the same column name more than once, even if you're using a PIVOT statement to turn the RoomIds form rows to columns.
You only can get the RoomIds at same level in a subset like this:
XML
<transaction>
  <propertydataset>
    <property>1</property>
      <Rooms>
        <roomid>51</property>
        <roomid>53</property>
        <roomid>65</property>
      </Rooms>
  </propertydataset>  
  <propertydataset>
    <property>2</property>
      <Rooms>
        <roomid>51</property>
        <roomid>481</property>
        <roomid>101739578</property>
      </Rooms>
  </propertydataset> 
</transaction>


Look here for how to create nested XML queries:
Nested FOR XML results with SQL Server’s PATH mode – Richard Dingwall[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900