|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Monday, April 01, 2013 10:11 AM
Points: 576,
Visits: 27,680
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 6:33 AM
Points: 1,080,
Visits: 687
|
|
| It was a bit fiddly but then in my experience that is the case when dealing with xml, especially in SQL.
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 9:56 AM
Points: 4,418,
Visits: 7,156
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 8:31 AM
Points: 1,146,
Visits: 1,448
|
|
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.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Wednesday, October 24, 2012 8:17 PM
Points: 1,558,
Visits: 247
|
|
My logic was totally wrong. The reverse axis steps on the correct answer didn't seem right to me.
http://brittcluff.blogspot.com/
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, March 16, 2013 9:53 AM
Points: 847,
Visits: 768
|
|
Thank you for the question
Iulian
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, January 27, 2012 6:27 AM
Points: 7,
Visits: 8
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Monday, April 01, 2013 10:11 AM
Points: 576,
Visits: 27,680
|
|
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?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 6:38 AM
Points: 1,326,
Visits: 420
|
|
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)
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 4:24 AM
Points: 1,158,
Visits: 642
|
|
| Lots of fuss and discussion about this question - isn't that a good thing?
|
|
|
|