XMLTABLE - XML/SQL Functionality

  • d_sysuk

    SSC Eights!

    Points: 809

    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

    SSChampion

    Points: 13482

    Thanks for posting the suggestion, I think that would be great and have voted for it.

  • Orlando Colamatteo

    SSC Guru

    Points: 182268

    +1

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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply