sql server 2008 r2 - How to access nested elements in xml with OpenXML -
i'm trying import data rather large xml file sql server. after online searching settled upon using openxml. query have far is
declare @doc int declare @xml xml select @xml = evnt openrowset (bulk 'c:\archive.xml', single_blob) import(evnt) exec sp_xml_preparedocument @doc output, @xml select * openxml( @doc, '/events/eventdata/event', 2) ( id varchar(max) ) exec sp_xml_removedocument @doc
in way i'm able elements listed inside 'event', how element 1 more level down 'event'. example how 'custid' tag below?
<event> <custromattribute> <custid>...
the items inside 'event' needed hence path cant changed. other approaches nice too. need run sql server can't use ssis or other similar external tools. xquery seems take lot of time.
i'm using sql server 2008 r2
if interested solution above question simple enough.
select * openxml( @doc, '/events/eventdata/event') ( id varchar(max) 'id' custid varchar(20) 'customattribute/custid', attributeid varchar(20) '@id' sampleid varhcar(20) 'ele1/ele2[3]/id' )
the values in quotes should match name in xml doc exactly. '@' used select attributes in element. going level done '../'.
the sampleid fetches 'id' within third 'ele2' element inside 'ele1'
Comments
Post a Comment