OpenRowSet command in TSQL is returning NULLS
Asked Answered
L

5

6

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$]')
Lara answered 26/8, 2009 at 12:26 Comment(1)
Now you know why ETL people hate Excel for imports!Foolish
R
6

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?

Ricker answered 26/8, 2009 at 12:34 Comment(2)
no problem, check out #1178743 for more infoLara
+1. A year after this was posted and it solved the same issue for me.Knucklehead
B
3

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.

Basidium answered 26/8, 2009 at 15:30 Comment(0)
I
1

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.

Inosculate answered 26/8, 2009 at 12:33 Comment(0)
E
0

I had the same problem. I fixed it cuting and pasting a row that contains a column with the string/numeric value (for example 123ABC) in the first row position of the sheet. For some reason T-SQL reads the first row and assumes that all the values are numeric.

Error answered 1/10, 2009 at 15:46 Comment(0)
E
0

Response by SqlACID in this link worked great [https://wikigurus.com/Article/Show/185717/OpenRowSet-command-in-TSQL-is-returning-NULLS] :-

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.

Exasperate answered 6/11, 2015 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.