XML File Load via OpenRowSet

  • I am using the following code to import an XML file via Sql Server. The XML file is verified to be properly formatted. If I import the entire file the results of the final SQL statement are empty. If I remove half the data in the file it imports fine. I have switched the top and the bottom half of data to rule out a character set issue. It seems to be a size issue? Has anyone had this problem? I can import the file with no issue via SSIS but I would rather use this method. Any help would be greatly appreciated.

    CREATE TABLE XmlImportTest

    (

    xmlFileName VARCHAR(300),

    xml_data xml

    )

    GO

    DECLARE @xmlFileName VARCHAR(300)

    SELECT @xmlFileName = '\\servername\e$\xml_test\PD_852_387401.xml'

    -- dynamic sql is just so we can use @xmlFileName variable in OPENROWSET

    EXEC('

    INSERT INTO XmlImportTest(xmlFileName, xml_data)

    SELECT ''' + @xmlFileName + ''', xmlData

    FROM

    (

    SELECT *

    FROM OPENROWSET (BULK ''' + @xmlFileName + ''' , SINGLE_BLOB) AS XMLDATA

    ) AS FileImport (XMLDATA)

    ')

    GO

    SELECT * FROM XmlImportTest

    DROP TABLE XmlImportTest

  • IIRC, the SINGLE_BLOB option returns the data as single-row, single-column value of type varbinary(max), and when using OPENROWSET to import LOB data that exceeds 8,000 bytes in length, you have to use a format file to specify the maximum length of the data. See here for more:

    http://msdn.microsoft.com/en-us/library/ms178129(SQL.90).aspx

    Jason Wolfkill

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

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