Search code examples
t-sqlexcelimport-from-excelopenrowset

OpenRowSet command in TSQL is returning NULLS


Been investigating for a while now and keep hitting a brick wall. I am importing from xls files into temp tables via the OpenRowset command. Now I have a problem where I’m trying to import a certain column has a range values but the most common are the following. Columns structured as long numbers i.e. 15598 and the some columns as strings i.e. 15598-E.

Now the openrowset is reading the string version no problem but is reporting the number version as a NULL. I read (http://www.sqldts.com/254.aspx ) that openrowset has that issue and the author speaks of implementing “HDR=YES;IMEX=1” into the query string but that’s not working for me at all.

Have any of you guys every encountered this?

Just some more info as well. I may not do this with the JET engine (Microsoft.Jet.OLEDB.4.0) so this is what my query looks like:

SELECT *
FROM 
    OPENROWSET('MSDASQL'
                , 'Driver=Microsoft Excel Driver (*.xls);HDR=YES;IMEX=1;DBQ=C:\ImportFile.xls;'
            , 'SELECT * FROM [Sheet1$]')

Solution

  • I notice you are using the Excel ODBC driver. Have you tried the JET OLEDB Provider with the equivalent connection string?

    select * from openrowset(
        'Microsoft.Jet.OLEDB.4.0',
        'Data Source=C:\ImportFile.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1"',
        'SELECT * FROM [Sheet1$]')
    

    EDIT: Sorry, just noticed your last paragraph. Surely the Excel ODBC driver still goes via the JET engine, so what difference would it make?

    EDIT: I have looked at the KB194124 link, and the registry values it recommends are the default values on my machine, which I have never changed. I have used the above method several times myself without problems. Maybe it's an environmental issue?