98.9% Of all customers recommend us, we're so confident about our results we publish all reviews and statsView Live Stats View Reviews
How To Use Excel To Create Large SQL Insert Statements
Wed 12th May 2010
In order to begin, work out the Insert statement required for the first record. As an example, let us assume we are entering a contact record into a database from a spreadsheet containing three separate columns for FirstName, Surname and PhoneNumber. In this example the Insert statement into Table_Contact is as follows:
INSERT INTO Table_Contact VALUES ('Sarah', 'Hanson', '5556543')
In order to create the insert statement for all the records on the spreadsheet, add an additional column formatted as text with the following value in each cell:
INSERT INTO Table_Contact VALUES ('
The clever part of the Excel sheet is the Concatenation function. This function allows you to merge together any cells along with text of your choice. It can join up to 30 strings together. The advantage of this is that it can automatically join together all the strings along with the SQL script to give the final full SQL insert statement. The user can then simply copy the final script into the SQL Query Analyser and press play to run it. If you were to use the formula editor to enter the following Concatenation function into a separate cell it would look as follows:
The result would be as follows:
INSERT INTO Table_Contact VALUES ('SarahHanson5556543
Since there is no separate column to include the quotation marks, spaces and commas required, excel has merged them altogether as requested which would result in a SQL error. There are two ways to deal with this issue. The first is to add columns and enter the missing characters as appropriate. This is often the simplest solution as it avoids complications in the formula and the user can clearly see what is to be merged. Fortunately Excel Concatenation provides a simpler answer for those who are willing to give it a go. You can enter these characters directly into the formula editor. Change the formula as follows:
=CONCATENATE(A2,B2,"', '",C2,"', ","'",D2,"')")
The cell with the formula should be formatted as General and the final result should be the complete and correct SQL Insert Into statement. This formula can simply be dragged down to all required rows in the column. Since dragging the formula will automatically update it to include the appropriate cells, you should be left with an individual Insert statement for each row including the correct information. This series of statements can simply be copied and pasted into the SQL Query Analyser. It is always worth taking a full copy of the database to use as a test database so you can test the scripts first. Possible problems may occur if you have names such as O'Hanson with an apostrophe. This may have to be manually adjusted.
Excel is an excellent resource for creating large SQL statements from data already in an excel spreadsheet. The example used here is very simple and the function may need refining for different data formats and other issues. Once the user is confident and fully understands the method it can be further improved by removing the column with the initial part of the Insert statement and simply adding this as text to the concatenation formula. The formula would then look as follows:
=CONCATENATE("INSERT INTO Table_Contact VALUES ('",B2,"', '",C2,"', ","'",D2,"')")
If there are huge amounts of data it may even be necessary to set up two concatenation functions with half the SQL Insert statement in each one. It is then possible to set up a separate concatenation function to merge them both. This will all depend on the level and understanding of the user and the type and amount of data.
Original article appears here:
London's widest choice in
dates, venues, and prices
On-site / Closed company:
Excel VBA Intermediate
I think will return very soon to learn more and improve my skills.
Jo Malone London
Global Education Assistant
Absolutely excellent course with Richard who was a funny, informative and extremely patient trainer. I will most certainly return for more courses trained by him and will recommend for my colleagues
Department For Transport
Excel Advanced - Formulas & Functions
It would be useful to have a section on advanced pivot tables and perhaps just make reference to average formulas