SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XMLTABLE - XML/SQL Functionality


XMLTABLE - XML/SQL Functionality

Author
Message
d_sysuk
d_sysuk
SSC-Enthusiastic
SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)SSC-Enthusiastic (171 reputation)

Group: General Forum Members
Points: 171 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
UMG Developer
UMG Developer
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4108 Visits: 2204
Thanks for posting the suggestion, I think that would be great and have voted for it.
Orlando Colamatteo
Orlando Colamatteo
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40960 Visits: 14413
+1

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


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search