Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 reading nodes from xml

Author  Topic 

jimoomba
Yak Posting Veteran

90 Posts

Posted - 2012-12-05 : 08:57:11
Hi All,

Iam having the xml as shown below :

<root>
<Ticket TicketID="64504" TransactionTypeID="1" QueueID="1">
<TicketMeta Name="Message">Test Case Desc</TicketMeta>
<TicketMeta Name="Element ID 1">1111</TicketMeta>
<TicketMeta Name="Element ID 2">2222</TicketMeta>
<TicketMeta Name="Element ID 3">3333</TicketMeta>
<TicketMeta Name="Element ID 3">3336</TicketMeta>
<TicketMeta Name="Element ID 4">4444</TicketMeta>
<TicketMeta Name="Element ID 5">5555</TicketMeta>
<TicketMeta Name="Element ID 2">2222</TicketMeta>
<TicketMeta Name="Element ID 3">3333</TicketMeta>
<TicketMeta Name="Element ID 3">3336</TicketMeta>
<TicketMeta Name="Element ID 4">4444</TicketMeta>
<TicketMeta Name="Element ID 5">5555</TicketMeta>
<TicketMeta Name="Event Window Start Date">11/26/2012</TicketMeta>
<TicketMeta Name="Event Window Start Time">03:00</TicketMeta>
<TicketMeta Name="Event Window End Date">11/26/2012</TicketMeta>
<TicketMeta Name="Event Window End Time">18:00</TicketMeta>
</Ticket>
</root>

And i want the output to display as follows using sql query for reading the nodes. and also the xml can contain any number of ticketmeta tags:

col1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15
64504 1 1 Test Case Desc 1111 2222 3333 3336 4444 11/26/2012 03:00 11/26/2012 18:00

kindly help me on the same

rams

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-05 : 10:03:02
quote:
also the xml can contain any number of ticketmeta tags
That makes it harder - you will need to shred the XML and then use dynamic pivot. Is that really the case, or can some boundaries/parameters be put on the number of elements?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-05 : 10:13:28
This is an example of how you can shred the XML when the number of elements you want to query are unknown - you will need to use dynamic pivot on this output using the RN column to get the data in the format you described
DECLARE @x XML = '<root>
<Ticket TicketID="64504" TransactionTypeID="1" QueueID="1">
<TicketMeta Name="Message">Test Case Desc</TicketMeta>
<TicketMeta Name="Element ID 1">1111</TicketMeta>
<TicketMeta Name="Element ID 2">2222</TicketMeta>
<TicketMeta Name="Element ID 3">3333</TicketMeta>
<TicketMeta Name="Element ID 3">3336</TicketMeta>
<TicketMeta Name="Element ID 4">4444</TicketMeta>
<TicketMeta Name="Element ID 5">5555</TicketMeta>
<TicketMeta Name="Element ID 2">2222</TicketMeta>
<TicketMeta Name="Element ID 3">3333</TicketMeta>
<TicketMeta Name="Element ID 3">3336</TicketMeta>
<TicketMeta Name="Element ID 4">4444</TicketMeta>
<TicketMeta Name="Element ID 5">5555</TicketMeta>
<TicketMeta Name="Event Window Start Date">11/26/2012</TicketMeta>
<TicketMeta Name="Event Window Start Time">03:00</TicketMeta>
<TicketMeta Name="Event Window End Date">11/26/2012</TicketMeta>
<TicketMeta Name="Event Window End Time">18:00</TicketMeta>
</Ticket>
</root>';


SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN,
TicketID,
TransactionTypeID,
QueueID,
c2.value('.','varchar(64)') AS val
FROM
(
SELECT @x.query('for $a in /root/Ticket/TicketMeta return $a' ) ,
@x.value('(/root/Ticket/@TicketID)[1]','varchar(32)'),
@x.value('(/root/Ticket/@TransactionTypeID)[1]','varchar(32)'),
@x.value('(/root/Ticket/@QueueID)[1]','varchar(32)')

)T1(c1,TicketID,TransactionTypeID,QueueID)
CROSS APPLY c1.nodes('/TicketMeta') T2(c2);
Go to Top of Page

jimoomba
Yak Posting Veteran

90 Posts

Posted - 2012-12-06 : 03:30:16
Thanks a lot sunita,
it really worked like a charm and this is what iam exactly looking for.
Once again thanks a lot

rams
Go to Top of Page

subashseo
Starting Member

4 Posts

Posted - 2013-01-19 : 07:17:37
unspammed
Go to Top of Page
   

- Advertisement -