98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsPrevious article Next article Excel articles
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:
=CONCATENATE(A2,B2,C2,D2)
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.
Author is a freelance copywriter. For more information on excel consultancy, please visit https://www.stl-training.co.uk
Original article appears here:
https://www.stl-training.co.uk/article-896-how-use-excel-create-large-sql-insert-statements.html
London's widest choice in
dates, venues, and prices
Public Schedule:
On-site / Closed company:
TestimonialsDementia UK
Insights & Evaluation Analyst Iain Marshall Power BI Reporting The course exceeded my expectations, which I think was down to Jens. Sometimes you worry that such courses may be a little dull, but Jens's enthusiasm for Power BI and what it can do really showed and made the course all that more engaging. The pace was just right, Jens was always clear with what he was doing at each step (repeating instructions to avoid people having to ask in case they missed it the first time which really helped keep the flow of the day). Jens was happy to answer any questions and checked in to make sure that we all understood and that the pace was fine. He made effort to come up with examples that related to the different areas of work that people on the course worked in. Admin Re UK
Solvency II Project Administrator Susan Quinn Excel Introduction Excellent course, trainer, restaurant and venue. Will highly recommend and hope to come back for Intermediate, etc. Where Workplace Works Ltd
Director Jackie Furey PowerPoint Intermediate Advanced Really informative day, learnt loads and will be able to do a lot for ourselves that we once outsourced. Thank you. I will be back. |
PUBLICATION GUIDELINES