Excel Macro Creates INSERT Statements for Easy Data Migration

Downloads
129367.zip

As part of my role as a hands-on data management advisor, I have to migrate data stored in legacy and nonrobust systems into a managed system environment. One of the most common formats I have to work with is that of Microsoft Excel. I prefer to review the data from large Excel worksheets and workbooks in SQL Server because I can use T-SQL queries to thoroughly check the data. To get the data into SQL Server, I had been using the Import Data feature of SQL Server Management Studio (SSMS) or building INSERT statements by concatenating values together in Excel expressions. However, both methods proved tedious, so I developed a solution that automatically builds INSERT statements.

My solution consists of an Excel macro named Create_Insert_Statements and a stored procedure named BSP_UTILITY_MatchColumnDataTypes. Here’s how the solution works. In the Excel worksheet whose data you want to export, you start the macro. The macro prompts you for a name for the table that will contain its output. The macro then constructs a CREATE TABLE statement using that table name and the worksheet’s column headers. The headers must be unique and can’t contain spaces. In addition, the worksheet’s first column needs to have contiguous values.

Next, the macro loops through the data, building INSERT statements for all the columns and rows. The INSERT statements, which look like

INSERT INTO <table name>

values (<value1>,…,n))

are written to a blank column (i.e., the third column past the last column containing data).

After the last INSERT statement is written, the macro copies the column to the clipboard. You then paste the INSERT statements into a New Query window in SSMS and execute them in any database (provided you have the necessary permissions). The result is a table and a command that you can use to run the BSP_UTILITY_MatchColumnDataTypes stored procedure. Because the macro saves all the columns as varchar(255), the stored procedure changes the data type of each column into a more appropriate type:

  • If the values are numeric, have no decimals, and fall between 2^31 and -2^31, the column data type is changed to int.
  • If the values are numeric but not integers, the column data type is changed to float.
  • If no values meet the criterion isdate()=0, the column data type is changed to date.
  • If the values don’t meet any of the aforementioned criteria, the varchar column is resized to the length of the longest value in the column.

You can download the code for the stored procedure (which is in the MatchColumnDataTypes.sql file) as well as the Create_Insert_Statements macro (which is in the Dynamic Database Insert Excel Tool.xls speadsheet) from the SQL Server Magazine website. Go to the top of this page and click the Download the Code Here button.

Besides the macro, the Dynamic Database Insert Excel Tool.xls spreadsheet contains sample data you can use to test the solution. To do so, follow these steps:

  1. Run MatchColumnDataTypes.sql to create the BSP_UTILITY_MatchColumnDataTypes stored procedure.
  2. Open Dynamic Database Insert Excel Tool.xls in Excel. Navigate to cell A2.
  3. Press Ctrl+Q.
  4. When the macro prompts you, enter a name for the output table.
  5. After the macro executes, paste the selected column into a New Query window in SSMS.
  6. Execute the code.
  7. In the results, find and uncomment the command that executes the BSP_UTILITY_MatchColumnDataTypes stored procedure. Manually execute that command to change the column data types.

I’ve tested this solution with SQL Server 2005 and Excel 2007. To view the macro’s code in Excel, press Alt+F8 to launch the Macro window. Select the Create_Insert_Statements macro and click Edit. If you’re unfamiliar with Excel macros, you can find information about them at the Microsoft Office web page.

With this solution, I can quickly import Excel data into SQL Server so that I can thoroughly review it using T-SQL. It’s not a perfect solution for all Excel data migration scenarios, but it greatly helps me with ad hoc reviews of large Excel data sets.

Discuss this Article 7

bevanward
on Apr 5, 2011
The editor told me that there is a problem with the website that they are addressing so hopefully downloads are up soon ...
bmemberg
on Apr 4, 2011
Where is the download for this article?
mah530
on Apr 6, 2011
This could be a very useful tool. Thank you for sharing it. But, WHERE is the download for this article?
bsblackmore
on Jun 2, 2011
I think people under estimate the use of Access in migrating Excel data to SQL. Link your access database to SQL instance, link your hold table in access to the excel sheet, and write a quick update query in access to move the data over. You can then build a macro to cycle through any number of imports. Also you can use relative links to capture spreadsheets anywhere the database lives.
STherrienEdudyn
on May 16, 2011
Just like you, I always disliked the tediousness of importing data from Excel into SQL. This article just made my week. I'm really looking forward to someone sending me an excel import now!
venky2307
on Apr 4, 2011
I do not see any downloadable links anywhere on this web page though the printed magazine already claims it? This is misleading...Please correct the issue on the site before putting it in black and white on paper...
dcarey14
on Apr 4, 2011
Where is the Download the Code Here button?

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.