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

XMLTABLE - XML/SQL Functionality Expand / Collapse
Author
Message
Posted Wednesday, March 02, 2011 9:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 09, 2013 6:28 AM
Points: 37, Visits: 227
Hi all,

Its not that often we get thanked by MS for suggesting things that they want to do, but have been unable to implement due to a lack of knowledge/customer demand in an area which would not be know about unless you have worked with other RDMBS vendor systems.
Without some help from fellow SQL Server professionals which do extensive work in XML - we would not get this useful functionality which MS want to give us and rid of us some non-intuitive proprietry commands.
[Apologies in advance - if this is not the right place to place this feedback]

This functionality is the XMLTABLE command (XML/SQL - standard) which gives the ability to shred XML Documents quickly with a lower amount of code required than the current .nodes() or OPENXML processing methodology.

Take the following XML Example:
DECLARE @xml XML
SELECT @xml =
'<dept bldg="101">
<employee id="901">
<name>
<first>John</first>
<last>Doe</last>
</name>
<office>344</office>
<salary currency="USD">55000</salary>
</employee>
<employee id="902">
<name>
<first>Peter</first>
<last>Pan</last>
</name>
<office>216</office>
<phone>905-416-5004</phone>
</employee>
</dept>
'


Current version: (using .nodes() - method) [267 chars]
SELECT xml_data.value('@id[1]','int') empID,
name_info.value('first[1]','varchar(max)') first_name,
name_info.value('last[1]','varchar(max)') last_name
FROM @xml.nodes ('dept/employee') xml1(xml_data)
CROSS APPLY xml_data.nodes ('name') name_list(name_info)



New version: (using Standardised XML/SQL XMLTABLE command) [217 chars]
SELECT X.* 
FROM emp,
XMLTABLE ('$d/dept/employee' passing doc as "d"
COLUMNS
empID INTEGER PATH '@id',
firstname VARCHAR(20) PATH 'name/first',
lastname VARCHAR(25) PATH 'name/last') AS X



Personally I prefer the cleaner XMLTABLE version for the following reasons:
* Standardised across all major RDBMS (IBM/ORACLE) etc [Easier to port from other systems to/from SQL Server]
* Cleaner code produced - reduces need for redundant code blocks (.nodes() / .value), allows for many multiple paths to be used without requiring cross apply joins within the code.
* Easier to read the XMLTABLE syntax - as its cleaner and matchs the paths more directly without having to cross-reference cross-apply joins.


Please show your support for this syntax as a requirement to our friends at MS; so we can get an improved way to quickly shred XML document in the future so this gets on the radar:
https://connect.microsoft.com/SQLServer/feedback/details/644485/xmltable-output
[For those who are interested in the full spec of the XML/SQL Standard check out the detail information on the feedback]

Thanks-in advance,
D
Post #1072023
Posted Saturday, March 12, 2011 3:48 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, March 27, 2014 8:50 AM
Points: 2,163, Visits: 2,184
Thanks for posting the suggestion, I think that would be great and have voted for it.
Post #1077378
Posted Friday, May 20, 2011 3:25 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 9:25 AM
Points: 7,070, Visits: 12,523
+1

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato

Believe you can and you're halfway there. --Theodore Roosevelt

Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein

The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein

1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
Post #1112779
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse