sql server - XPath query: parsing multiple paths using the same query (Cross Apply / .nodes() ) -


i have rather big , structured xml receipt 1 want parse relational database. there equal structures on different levels, it'd parse them using same sql statement. like:

declare @xmlpath varchar(127) set @xmlpath = 'atag/btag/item'  insert xmlreaditems   select ci.invoiceid,      t.c.value('productname[1]', 'varchar(63)') invoicetarget,     t.c.value('unit[1]', 'varchar(15)') unit, @xmlitems ci cross apply xmlcol.nodes(*[local-name()=sql:variable("@xmlpath")]') t(c) 

where @xmlpath string variable or field table (what using sql:column()?). of them couldn't make work. can use static string in xmlcol.nodes().

there no way can construct xquery parameter dynamically limited literal string only. see msdn says parameter of nodes() xml method :

xquery

is string literal, xquery expression. if query expression constructs nodes, these constructed nodes exposed in resulting rowset. if query expression results in empty sequence, rowset empty. if query expression statically results in sequence contains atomic values instead of nodes, static error raised.

forcing pass sql variable nodes() method trigger error :

the argument 1 of xml data type method "nodes" must string literal.

the trick you're trying implement works matching element name dynamically, not constructing entire xpath dynamically. example, following should work fine shred on item elements :

set @elementname = 'item'  select ..... @xmlitems ci  cross apply xmlcol.nodes('//*[local-name()=sql:variable("@elementname")]') t(c) 

in end there no workaround limitation far can see, unless want go farther construct entire query dynamically (see: sp_executesql).


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -