Link to home
Start Free TrialLog in
Avatar of tingleweb
tinglewebFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Import from Excel to SQL Server causing problems

Hello all.
I've seen lots of questions relating to this subject so please excuse me for repeating question but....

I am using :
SELECT * INTO Product FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\Websites\d2v\D2V-database.xls', 'SELECT * FROM [Sheet1$]')
to import frm an Excel spreadsheet but its causing me problems.

Its bringing in about 200 extra rows than are actually populated in Excel and some of the columns dont import correctly.  I also need to drop the table first for the import to happen which I'd rather not do due to table relationships, etc.

What is the correct script I should be using please?
Avatar of YZlat
YZlat
Flag of United States of America image

I suggest that instead of SELECT * do SELECT Column1, Column2 ... etc.
ASKER CERTIFIED SOLUTION
Avatar of matrix_aash
matrix_aash
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if it works for you, my preferred approach is to send the data to SQL Server from Excel, using VBA and linking to the database with ADO.

This then gives you much more control over the data in Excel.

Your SELECT ... INTO ... FROM ... is useful for creating a table first time around, but after that, you should get the data you need from a consistently formatted Excel sheet with INSERT...FROM...

You can use 'SELECT * FROM [Sheet1!A1:Z100$]' to select the cells to be transferred.

alternatively, as suggested above, use SSIS (2005) / DTS (2000) and create a package to do the job...
I find this way prefferable:

INSERT INTO YourTable
      (field1, field2,...)
SELECT
      field1, field2,...
FROM
      OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;
                        Database=D:\Websites\d2v\D2V-database.xls', Sheet1$) excel


The reason your columns are imported correctly is because they have heterogenous data in them, numeric and alphanumeric. You have to make sure that all the values in a column are either numeric or alphanumeric.

You don't have to DROP the table but you can use:

TRUNCATE TABLE YouTable;

This keeps all yor defaults, relationsheips and resets identity column if you have one. Is also very fast compared to DELETE FROM YourTable if the table is big.