Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

XML to Relational Expand / Collapse
Author
Message
Posted Friday, January 27, 2012 3:55 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, March 10, 2014 9:39 AM
Points: 579, Visits: 27,690
Very useful question, thanks. I learnt something about xml parsing and also optimization so far today :). Thanks for all the comments.
I would also like to defend the question. I think seeing unconventional ways of solving a problem are always helpful. When the task is to review existing code, it is helpful to know what is working (although not optimal).

Cheers
Post #1242727
Posted Friday, January 27, 2012 4:23 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:39 AM
Points: 1,775, Visits: 1,169
It was a bit fiddly but then in my experience that is the case when dealing with xml, especially in SQL.
Post #1242732
Posted Friday, January 27, 2012 4:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, October 31, 2014 11:06 AM
Points: 5,430, Visits: 10,108
Stewart "Arturius" Campbell (1/27/2012)
KWymore (1/27/2012)
Stewart,

One thing to note. Answer 2 and 3 don't match the case for columns "name" and "componentname" as declared in the example code. Since I know little of XML, I took a stab at this question by eliminating those 2 answers first based solely on the columns not matching and then tried to figure it out. I got lucky and answered correctly.

Not sure how many others would narrow down their response this way but I thought I would bring it to your attention anyways.


Thanks for this. I have checked, and found that the only place where the column names differ is in the alias assignment of the relevant lines.
The relevant element names, as used in the tab1.col1.value() method, are correct.

Yes, but the fact that the case used in the aliases is not consistent throughout means that answers 2 and 3 cannot possibly give the required result set. I also managed to get it right by eliminating those two and taking a guess.

John
Post #1242735
Posted Friday, January 27, 2012 5:28 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 10:03 AM
Points: 1,895, Visits: 2,194
SQL Kiwi (1/26/2012)
I dislike questions that make a parser/syntax checker out of the reader, especially where there is little in the way of learning value


As soon as I saw the question, I had the same reaction. By the time I finished the first pass through the code, I didn't care what the answer was. Glad I don't have to work with XML!


Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
Post #1242760
Posted Friday, January 27, 2012 6:12 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 7:45 PM
Points: 1,589, Visits: 253
My logic was totally wrong. The reverse axis steps on the correct answer didn't seem right to me.

http://brittcluff.blogspot.com/
Post #1242776
Posted Friday, January 27, 2012 6:21 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, November 13, 2014 3:36 AM
Points: 1,012, Visits: 1,000
Thank you for the question

Iulian
Post #1242782
Posted Friday, January 27, 2012 6:27 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 6:42 AM
Points: 7, Visits: 10
Yes, I got it right!
Post #1242786
Posted Friday, January 27, 2012 7:14 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, March 10, 2014 9:39 AM
Points: 579, Visits: 27,690
SQL Kiwi (1/27/2012)
That's the same as the code I posted (just without the /text() optimization).


I don't understand what this /text() optimization does. On my system it makes the example query 4 times faster. Why is that? Does it performs better for any sql data type?
Post #1242829
Posted Friday, January 27, 2012 7:51 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, November 13, 2014 11:41 AM
Points: 1,393, Visits: 485
I really like this question since I use a lot of XML for bulk inserts of business objects.

And thank you to Paul and everyone, I just learned that Cross Apply method seems to be about 5x faster. Never though of that method.

DECLARE @x xml

SET @X =
N'<toys>
<myToy>
<name>Train</name>
<components>
<part><name>engine</name><howMany>1</howMany></part>
<part><name>coaches</name><howMany>10</howMany></part>
<part><name>caboose</name><howMany>1</howMany></part>
<part><name>tracks</name><howMany>125</howMany></part>
<part><name>switches</name><howMany>8</howMany></part>
<part><name>power controller</name><howMany>1</howMany></part>
</components>
</myToy>
<myToy>
<name>remote control car</name>
<components>
<part><name>remote control</name><howMany>1</howMany></part>
<part><name>car</name><howMany>1</howMany></part>
<part><name>batteries</name><howMany>8</howMany></part>
</components>
</myToy>
</toys>
';


SELECT
[name] = T1.Toy.value('name[1]','VARCHAR(20)'),
componentname = T2.Part.value('name[1]','VARCHAR(20)'),
numberOfItems = T2.Part.value('howMany[1]','TINYINT')
FROM @x.nodes('/toys/myToy') T1(Toy)
CROSS APPLY
T1.Toy.nodes('./components/part') T2(Part)


SELECT
[name] = T2.Part.value('../../name[1]','VARCHAR(20)'),
componentname = T2.Part.value('name[1]','VARCHAR(20)'),
numberOfItems = T2.Part.value('howMany[1]','TINYINT')
FROM @x.nodes('/toys/myToy/components/part') T2(Part)




Post #1242863
Posted Friday, January 27, 2012 8:00 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 8:30 AM
Points: 1,417, Visits: 806
Lots of fuss and discussion about this question - isn't that a good thing?
Post #1242874
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse