Trick: Importing data from Excel using INSERT statements…

To Import data from excel to Sql table using SQL Import/Export tool is simple and very effective. But we can import large tables into Sql table by generating insert statements in excel sheet. Here is the trick that i found very interesting and faster than Import/Export wizard.

This approach also requires the destination table to be created manually or it should already exist.

  1. Run the below script to create destination table and insert some data in it.

The result of last Query is :

 

 

 

  • The source Excel file contains below data that we want to import in Test1 table:

 

 

 

 

  • Now to convert this to INSERT statement, we need to add a few columns to Excel sheet which will contain these texts ” Insert Into Test1 VALUES (‘ “, ” ” “,” ‘ ” etc. as shown below:

 

 

 

 

  •  Then you need to concatenate these columns using Excel’s CONCATENATE function:

  • Finally, copy the same formula to all rows, these are the INSERT statements you need!:

  • Now copy them to SSMS and execute them. We have now imported the Excel file data into table :).

Reference : http://www.sqlservercentral.com/blogs/sqlandme/2012/03/29/importing-data-from-excel-using-insert-statements/

Advertisements

About Ashish Jain

I am Ashish Jain, a software engineer by profession. My goal of creating this blog is to share my knowledge of SQL server with all other SQL enthusiasts and also to learn from them.
This entry was posted in T-SQL and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s