Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Excel Spreadsheet Data to an EXISTING sql table?

Tags:

I have a table called tblAccounts whose contents will come from an excel spreadsheet.

I am using MS SQL Server 2008 (x64) on a Windows 8.1 (x64)

I tried using the SQL Server Import/Export Wizard but there is no option to choose an existing table but only an option to create a new one.

I tried using other methods such as OPENROWSETS

INSERT INTO tblAccount SELECT * FROM OPENROWSET( 'Microsoft.Jet.OLEDB.4.0', 'Excel 12.0;Database=D:\exceloutp.xls','SELECT * FROM [Sheet1$]') 

but gave me an error:

Msg 7308, Level 16, State 1, Line 1 OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode.

Some research told me that it occurred because of a 64-bit instance of SQL server.

The problem is that this Excel data transfer to a SQL table must be accomplished using the SQL Import/Export Wizard only.

How can I import an Excel spreadsheet to an existing SQL table without creating a new one?

Some links I visited but was not able to help me resolve my problem:

  • How do I import an excel spreadsheet into SQL Server?
  • Fix OLE DB error
like image 299
Saudate Avatar asked Nov 20 '13 00:11

Saudate


People also ask

How do I automatically import data from Excel to SQL Server?

One time: could right click database instance and choose Task-> Import Data. Automatic: build SSIS package and schedule job in SQL server to run ETL process.


1 Answers

You can copy-paste data from en excel-sheet to an SQL-table by doing so:

  • Select the data in Excel and press Ctrl + C
  • In SQL Server Management Studio right click the table and choose Edit Top 200 Rows
  • Scroll to the bottom and select the entire empty row by clicking on the row header
  • Paste the data by pressing Ctrl + V

Note: Often tables have a first column which is an ID-column with an auto generated/incremented ID. When you paste your data it will start inserting the leftmost selected column in Excel into the leftmost column in SSMS thus inserting data into the ID-column. To avoid that keep an empty column at the leftmost part of your selection in order to skip that column in SSMS. That will result in SSMS inserting the default data which is the auto generated ID.

Furthermore you can skip other columns by having empty columns at the same ordinal positions in the Excel sheet selection as those columns to be skipped. That will make SSMS insert the default value (or NULL where no default value is specified).

like image 180
yash vora Avatar answered Sep 29 '22 11:09

yash vora