Home Forums Programming XML Reading XML File into a table RE: Reading XML File into a table

  • sp_xml_preparedocument doesn't read a file from disk, it creates a handle to an internal XML document (variable). To read the attached XML file into a variable, see this link[/url], or (rather more simply), follow LutzM's advice and use OPENROWSET().

    DECLARE @XML xml

    SELECT @xml = x.a

    FROM OPENROWSET(BULK 'C:\Med_Form.xml', SINGLE_BLOB) AS x(a)

    --either query with OPENXML()

    DECLARE @XMLDocPointer INT

    EXEC sp_xml_preparedocument @XMLDocPointer OUTPUT, @xml

    SELECT * FROM OPENXML(@XMLDocPointer,N'/document/med_Form/medFormID', 0)

    -- or with XQuery

    SELECTdoc.medFormID.value('.[1]','int') AS ID

    ,doc.medFormID.value('../FormName[1]','varchar(60)') AS FormName

    FROM@xml.nodes('document/med_Form/medFormID') doc(medFormID)