Export Excel table to sql using VBA - vba

Can anyone point me to an example for exporting an excel table into an sql table, assuming the same structure, using VBA? I'm using Excel 2007. I've seen examples that read the data one row at a time and either run one insert per row, or build their recordset, and do UpdateBatch. Is there a way where I can select all record from the source table in excel and insert them all into the target sql table?

Bulk Insert is what you are looking for.
Try these:
Stackoverflow question
Bulk Insert - social msdn
Other way is to import in sql management studio
Import Excel spreadsheet columns into SQL Server database

Related

How to Bulk Insert to a SQL table from Lotusscript Agent

I need upload over 10,000 records into a sql table from a lotusscript agent. Is there a way I can bulk load them? Excel file attachment is an option if I can do it that way. But I have to use lotusscript agent.
Refer to the answers to this question: Insert multiple rows WITHOUT repeating the "INSERT INTO ..." part of the statement?
First figure out what the correct syntax for providing values to a multiple record INSERT is in the particular SQL dialect that you are working with, and then write your LotusScript code to read your data values (from Excel, or from whatever the actual source is) and produce insert them into the right places in a string containing your SQL INSERT syntax. Then submit that SQL string to your database server.

Generating SQL INSERT from data in Excel file

I have a spreadsheet with thousands of records. Columns correspond to fields in a table in a SQL Server database. What is the quickest way to generate an SQL query to insert this data?
#Al2110
based on your query, To generating SQL insert from data in excel file that show in below:
insert into customers values('" &B3 &"','" &C3& "','"&D3&"');
I hope above information will be useful for you.
Thank you.
SQL Server Import and Export Wizard is a convenient option.

Best practice for data migration

Currently We have developed a system for a manual work they have been doing using many excel files.
Is there a best practice for data migration? because I wanted to use backend language like .net to do the validation and insert into tables rather than using SQL to do migration.
Total record in excel is around 12K rows but for many tables so its not needed consider a lot about performance and it is only one time.
I would add a few calculated columns in Excel that would generate SQL Insert / Update scripts. Something like ="INSERT INTO table (column) VALUES ('"&A1&"');"
Then just copy calculated column and run it through SQL client. I used to have a macros to run it directly from Excel through OLEDB that would highlight failed expressions and store SQL Exceptions next to them.
That way the data can be easily tidied, corrected and SQL re-run as needed.

how to import data from sql to excel and update sql data

for me user interface is excel sheet. so i want functionality such that i can import data from sql table.
and when i update data in excell sheet it shoul be update in sql table also.
can any one give me solution??
Similar questions have been asked. See this:
Two-Way Excel to SQL Connection
MS Access is a more appropriate tool for SQL Server connections, but if you have to use Excel then follow those links.
Hope that helps!

Import Excel Spreadsheet Data to an EXISTING sql table?

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
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).
Saudate, I ran across this looking for a different problem. You most definitely can use the Sql Server Import wizard to import data into a new table. Of course, you do not wish to leave that table in the database, so my suggesting is that you import into a new table, then script the data in query manager to insert into the existing table. You can add a line to drop the temp table created by the import wizard as the last step upon successful completion of the script.
I believe your original issue is in fact related to Sql Server 64 bit and is due to your having a 32 bit Excel and these drivers don't play well together. I did run into a very similar issue when first using 64 bit excel.
You can use import data with wizard and there you can choose destination table.
Run the wizard. In selecting source tables and views window you see two parts. Source and Destination.
Click on the field under Destination part to open the drop down and select you destination table and edit its mappings if needed.
EDIT
Merely typing the name of the table does not work. It appears that the name of the table must include the schema (dbo) and possibly brackets. Note the dropdown on the right hand side of the text field.
If you would like a software tool to do this, you might like to check out this step-by-step guide:
"How to Validate and Import Excel spreadsheet to SQL Server database"
http://leansoftware.net/forum/en-us/help/excel-database-tasks/worked-examples/how-to-import-excel-spreadsheet-to-sql-server-data.aspx