Shred XML data stored in BULK/BLOB table



Hello Stack community,


Per my code below I am having difficulty in successfully shredding xml data once it has been bulk inserted into a table.


I have found that most all examples dealing with xml on this site and the web define the xml right in the sproc, when working from an example like that I can achieve the result I want. However it seems that things become difficult when I pull the xml from a table again similar to my code below. An answer that derives the xml data from a definition (i.e. @xml = 'Root..../Root';) is not going to work, I need to have the xml being called from a table.


What I have so far can be found below.


CREATE TABLE XMLwithOpenXML ( Id INT IDENTITY PRIMARY KEY, XMLData XML, LoadedDateTime DATETIME )


INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime) SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() FROM OPENROWSET(BULK 'F:\stage\xmlfile.xml', SINGLE_BLOB) AS x;


DECLARE @xml as XML DECLARE @hDoc as INT DECLARE @SQL nvarchar(max)


SELECT @xml = xmldata FROM XMLwithOpenXML


exec sp_xml_preparedocument @hDoc OUTPUT, @xml


select AccountID from OPENXML(@hDoc, '/report/DataRecord') with ( AccountID varchar '@AccountID' )


exec sp_xml_removedocument @hDoc


No comments:

Post a Comment