Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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$]')
like image 283
StevenMcD Avatar asked Aug 26 '09 12:08

StevenMcD


3 Answers

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?

like image 118
Christian Hayter Avatar answered Sep 28 '22 01:09

Christian Hayter


If you don't mind opening the file in Excel, take the columns that have the problem, select the column, and do

Data -> Text to Columns -> Next -> Next -> Text

Save the spreadsheet and they should all come in as Text in OPENROWSET

I've found using .CSV files instead of Excel, opened by setting up a Linked Server, and setting up the format of the files in schema.ini a more practical approach for handling imports like this, with that method you can explicitly choose each column's format.

like image 41
SqlACID Avatar answered Sep 28 '22 02:09

SqlACID


We've come across the same issue. Unfortunately we've not found a solution either. There's more information here which indicates that there might be a registry fix.

like image 40
nickd Avatar answered Sep 28 '22 02:09

nickd