Microsoft Office TrainingThe UK's Number 1 for Microsoft Office Training Sitemap add this page to your favourites/bookmarksBookmark page
 
view a printable version of this pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

articleHow To Use Excel To Create Large SQL Insert Statements

When adding records to a SQL database, data is often given in the format of an excel spreadsheet and it is up to the user to import the data into the database. It is impractical and time consuming to update an individual insert statement for each record. Instead, use excel as a resource to quickly and simply create a series of Insert statements which will also minimise human error.
When adding records to a SQL database, simple 'Insert Into' statements can be used. This will add one record per Insert statement. It may be the case that there is no user-friendly import system set up and the records must be entered manually with this method. It is not always the case that records are received one at a time. Sometimes a large amount of data is given all at once, particularly if the database is in the initial set up stage. Data is often given in the format of an excel spreadsheet and it is up to the user to import the data into the database. It would be impractical and time consuming to update an individual insert statement for each record. Instead it is possible to use excel as a resource to quickly and simply create a series of Insert statements which will also minimise human error.

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 http://www.microsofttraining.net


Original article appears here:
http://www.microsofttraining.net/article-896-how-use-excel-create-large-sql-insert-statements.html


Back to article list

Distribution notes

PUBLICATION GUIDELINES

  • You have permission to publish this article for free providing the "About the Author" box is included in its entirety.
  • Do not post/reprint this article in any site or publication that contains hate, violence, porn, warez, or supports illegal activity.
  • Do not use this article in violation of the US CAN-SPAM Act. If sent by email, this article must be delivered to opt-in subscribers only.
  • If you publish this article in a format that supports linking, please ensure that all URLs and email addresses are active links, without the rel='nofollow' tag.
  • Software Training London Ltd. owns this article. Please respect the author's copyright and above publication guidelines.
  • If you do not agree to these terms, please do not use this article.

Rate this page:
2.8/5 (129 votes cast)
Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses
Microsoft Certified Partner
Security Seal verified by visa, mastercard securecard

Mini sitemap. These are the main areas of our web site. Full sitemap.

Management training

Professional Skills courses
Project Management Course London
Project Management Courses London
Project Management Training London
Project Management Training
Project Seminar
Project Seminars
Time Management Course London
Time Management London
Time Management Courses London
Time Management Training London
Introduction to Finance course
Assertiveness Skills course
Effective Communications Skills training
Presentation Skills London

Training Formats

Public scheduled courses
On-site training
Closed company courses

Consultancy
Application Development

Blogs

Excel Training
MS Project Training
Microsoft Training Blog

Version differences

Office 2010 vs 2007
MS Project version differences

Training Information

London Computer Training
Computer Training London
Docklands Training Courses
Docklands Training London

Training venues London
Client list
FAQ
Pricing and availability
Course details / Syllabus

Training Articles
Training Information

Microsoft training

Microsoft Office training
& IT Applications

Microsoft Project training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training
MS Project courses
MS Project training
Outlook courses
PowerPoint courses
PowerPoint training
VBA courses
Word courses
Microsoft.training
(more...)

Excel Training

Excel courses
Excel Training Courses Medway
Autonumber in Excel
Microsoft Excel training
Basic Excel Courses
Basic Excel Course
Basic Excel Training

Interested in MS Access training?

Access courses
Microsoft Access training
Microsoft access courses
Microsoft training access course
Microsoft+access+training
Access courses in london

Training provider

Training providers
IT training companies
IT training providers
Management Training providers
Management Training provider

Event history, feedback results
Events in 2012 · 2011 · 2010 · More

See also

Crystal Reports training