|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 3:48 AM
Points: 3,125,
Visits: 4,311
|
|
Comments posted to this topic are about the item XML to Relational
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 8:43 PM
Points: 10,989,
Visits: 10,533
|
|
I dislike questions that make a parser/syntax checker out of the reader, especially where there is little in the way of learning value.
It is sad the 'correct' answer includes reverse axis steps ('../') since these are best avoided in practice. Hiding a cross join in code that is closer to the 'right' solution makes me wonder if the author was more concerned with achieving a low '% correct' figure that teaching the reader anything about XQuery or XML. I would have expected the correct answer to be closer to this:
SELECT name = tab1.col1.value('(./name/text())[1]', 'varchar(20)'), componentName = tab1.col1.value('(./name/text())[1]', 'varchar(20)'), numberOfItems = tab1.col1.value('(./howMany/text())[1]', 'tinyint') FROM @x.nodes('toys/myToy') tab (col) CROSS APPLY tab.col.nodes('./components/part') AS tab1 (col1);
For anyone that wants to play with the XML, I think this is about right:
DECLARE @x xml = 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> ';
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 6:51 AM
Points: 9,373,
Visits: 6,470
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 3:48 AM
Points: 3,125,
Visits: 4,311
|
|
SQL Kiwi (1/26/2012) Hiding a cross join in code that is closer to the 'right' solution makes me wonder if the author was more concerned with achieving a low '% correct' figure that teaching the reader anything about XQuery or XML.
The aim was to illuminate on a badly documented subject within MSSQL, not "trick" people.
Another possiblity for a right answer could have been:
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)
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 6:51 AM
Points: 9,373,
Visits: 6,470
|
|
Stewart "Arturius" Campbell (1/27/2012)
SQL Kiwi (1/26/2012) Hiding a cross join in code that is closer to the 'right' solution makes me wonder if the author was more concerned with achieving a low '% correct' figure that teaching the reader anything about XQuery or XML. The aim was to illuminate on a badly documented subject within MSSQL, not "trick" people. Another possiblity for a right answer could have been: 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)
I like this answer better, as it is more readable (personal opinion of course).
How to post forum questions. Need an answer? No, you need a question. What’s the deal with Excel & SSIS?
Member of LinkedIn. My blog at LessThanDot.
 MCSA SQL Server 2012 - MCSE Business Intelligence
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 8:43 PM
Points: 10,989,
Visits: 10,533
|
|
Stewart "Arturius" Campbell (1/27/2012) The aim was to illuminate on a badly documented subject within MSSQL, not "trick" people. Benefit of the doubt then.
Another possiblity for a right answer could have been: 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)
That's the same as the code I posted (just without the /text() optimization). Query plans below: Query above:
 Previous:
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 5,237,
Visits: 7,044
|
|
I knew I'd probably get this wrong, as I know very little about XML, and I'm not actively seeking to change that. But from what I picked up over time and some logical thinking, I managed to eliminate three of the four answer options.
Unfortunately, logical thinking is not always a guarantee for correctness. I rejected the correct answer because of the reverse axis steps. I expected them to be disallowed. Pauls reply taught me that this is not recommended practice, so my logical feeling was not too far off...
If the version Paul posted had been included in the answers, I would have spotted the difference with the answer I chose (b), and I would probably have come to the correct conclusion.
Ah well. No point for me today, but at least I got the reassurance that my limited knowledge and gut feelings about XML were not that far off.
Oh, and thanks Paul for the extra nugget about the /text() optimization - if I even do get a need to handle XML, I'll try to remember that!
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 3:48 AM
Points: 3,125,
Visits: 4,311
|
|
Thanks for the optimisation hint, Paul. I will definitely be investigating the impact of applying this to the XML used in our environment (which is quite extensive). So the question poser learns from another...
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 1:57 PM
Points: 2,575,
Visits: 1,533
|
|
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.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 3:48 AM
Points: 3,125,
Visits: 4,311
|
|
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.
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|