149

I have a large excel worksheet that I want to add to my database.

Can I generate an SQL insert script from this excel worksheet?

user2323240
  • 1,607
  • 2
  • 13
  • 15
  • You can also use my VBA macro to generate SQL commands. More here: http://stackoverflow.com/questions/1570387/how-to-insert-data-from-an-excel-sheet-into-a-database-table/37409790#37409790 – Petrik Dec 19 '16 at 12:22
  • If you don't want to code this / maintain it, [this app automatically](https://www.wax.run/) `INSERT`s / `UPDATE`s rows from Sheets to SQL – howMuchCheeseIsTooMuchCheese Dec 08 '21 at 12:09

13 Answers13

260

I think importing using one of the methods mentioned is ideal if it truly is a large file, but you can use Excel to create insert statements:

="INSERT INTO table_name VALUES('"&A1&"','"&B1&"','"&C1&"')"

In MS SQL you can use:

SET NOCOUNT ON

To forego showing all the '1 row affected' comments. And if you are doing a lot of rows and it errors out, put a GO between statements every once in a while

Hart CO
  • 34,064
  • 6
  • 48
  • 63
  • 3
    I was using `=CONCATENATE()` but using the `&` sign leads to much better readability! – mastazi Sep 30 '15 at 01:07
  • 2
    @mastazi Agreed! I switched to `&` when I bumped into the parameter limit on `CONCATENATE()` a while back, that isn't a common problem now that the limit is 255 parameters, but I never think about reverting. – Hart CO Sep 30 '15 at 01:57
  • 1
    CONCATENATE() is the way to go. Just click the fx icon on the formula bar to see what you have entered. It is much cleaner than just use & sign. Of course to use & sign is cool but sometime when you have double quotes or single quote, that will make you count forever of what is missing. – ian0411 Nov 02 '16 at 19:51
  • 1
    Doesn't work when having quotes in cells. Check my answer for a corrected version. – Simon Baars Jul 07 '17 at 10:25
  • @SimonBaars Good note, of course, if it wasn't simple/clean data I'd probably just export from excel to csv and import that rather than going through the extra steps in excel to clean it up. – Hart CO Jul 07 '17 at 13:59
  • @HartCO How to insert number( integer) in values? – Preshan Pradeepa Nov 29 '17 at 04:35
  • 2
    @PreshanPradeepa You can use the same syntax, SQL Server doesn't care if the integer is in quotes as long as it's a valid integer. – Hart CO Nov 29 '17 at 05:03
  • This answer should be made "sticky", staying on top of SO): So much excel driven development in practice. – Beytan Kurt Mar 01 '19 at 12:52
  • @HartCO, how can I add the order number – user123456 Mar 31 '19 at 05:21
  • @user123456 Might be best to ask a new question with some details, the above would work with any fields unless there are constraints that prevent direct inserts. – Hart CO Mar 31 '19 at 13:47
35

You can create an appropriate table through management studio interface and insert data into the table like it's shown below. It may take some time depending on the amount of data, but it is very handy.

enter image description here

enter image description here

Andrey Morozov
  • 7,839
  • 5
  • 53
  • 75
  • Handy. I see that with an IDENTITY column, the copy buffer needs to have the column, even though data in it will be ignored as the values are autogenerated as the rows are inserted. – fortboise Mar 19 '15 at 20:39
  • You can make your IDENTITY column the last in the columns list and then it would not be necessary to have an extra column at all. – Andrey Morozov Mar 20 '15 at 06:48
  • 1
    if you dont specify the IDENTITY values in the excel sheet; you can change and re-run the edit top 200 rows sql script by removing IDENTITY column. So when you copy and paste the values without IDENTITY; this method will work. – aozan88 Oct 10 '16 at 14:25
  • This is broken for dates and time fields in the Excel for anyone else that runs into the problem. The datetime data gets converted to "binary" data on the SQL side for some reason. – Kevin Vasko Jan 23 '17 at 16:18
  • That's not true :) You just need to use appropriate for your system date/time format – Andrey Morozov Jan 23 '17 at 19:03
  • For IDENTITY - remember to include an extra blank left-most column. This will allow proper seeding. For reference: https://sqlspreads.com/2017/05/04/how-to-insert-data-in-excel-to-sql-server/ – Jay Jan 30 '18 at 17:18
  • @AndreyMorozov May I add further detail to your answer via "edit" - for example, I would like to a few steps that would show how to format dates, how to enclose strings in single-quotes (including the date values) - and how to modify the sql to omit an Identity Seed column. Please advise. – qxotk Feb 22 '19 at 16:52
  • 1
    @condiosluzverde I would advise you to post your own answer. In case of editing this answer your changes will probably be rejected by community moderators. – Andrey Morozov Feb 22 '19 at 22:03
35

There is a handy tool which saves a lot of time at

http://tools.perceptus.ca/text-wiz.php?ops=7

You just have to feed in the table name, field names and the data - tab separated and hit Go!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
mhn
  • 2,660
  • 5
  • 31
  • 51
  • 23
    Don't post your sensitive data on the web. You have no way of knowing what happens to the data you submit to the web application. Some of these tools store your data and make it publicly available – wi2ard Feb 20 '18 at 10:49
21

You can use the following excel statement:

="INSERT INTO table_name(`"&$A$1&"`,`"&$B$1&"`,`"&$C$1&"`, `"&$D$1&"`) VALUES('"&SUBSTITUTE(A2, "'", "\'")&"','"&SUBSTITUTE(B2, "'", "\'")&"','"&SUBSTITUTE(C2, "'", "\'")&"', "&D2&");"

This improves upon Hart CO's answer as it takes into account column names and gets rid of compile errors due to quotes in the column. The final column is an example of a numeric value column, without quotes.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Simon Baars
  • 1,877
  • 21
  • 38
5

Depending on the database, you can export to CSV and then use an import method.

MySQL - http://dev.mysql.com/doc/refman/5.1/en/load-data.html

PostgreSQL - http://www.postgresql.org/docs/8.2/static/sql-copy.html

jmhobbs
  • 116
  • 7
  • 3
    MS SQL, you can use SSMS's import wizard to import an Excel File. – Hart CO May 29 '13 at 16:17
  • 1
    To add details to @HartCO - To navigate to SSMS Import Wizard right-click on database, hover over tasks, click on "Import Data..." near the bottom of the context menu. Follow the steps in the wizard. – qxotk Feb 22 '19 at 16:59
5

Use the ConvertFrom-ExcelToSQLInsert from the ImportExcel in the PowerShell Gallery

NAME
    ConvertFrom-ExcelToSQLInsert
SYNTAX
    ConvertFrom-ExcelToSQLInsert [-TableName] <Object> [-Path] <Object> 
      [[-WorkSheetname] <Object>] [[-HeaderRow] <int>] 
      [[-Header] <string[]>] [-NoHeader] [-DataOnly]  [<CommonParameters>]
PARAMETERS
    -DataOnly
    -Header <string[]>
    -HeaderRow <int>
    -NoHeader
    -Path <Object>
    -TableName <Object>
    -WorkSheetname <Object>
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
ALIASES
    None
REMARKS
    None
EXAMPLE
    ConvertFrom-ExcelToSQLInsert MyTable .\testSQLGen.xlsx
philu
  • 795
  • 1
  • 8
  • 17
SteveC
  • 15,808
  • 23
  • 102
  • 173
2

You could use VB to write something that will output to a file row by row adding in the appropriate sql statements around your data. I have done this before.

Josh
  • 414
  • 6
  • 14
1

Here is another tool that works very well...

http://www.convertcsv.com/csv-to-sql.htm

It can take tab separated values and generate an INSERT script. Just copy and paste and in the options under step 2 check the box "First row is column names"

Then scroll down and under step 3, enter your table name in the box "Schema.Table or View Name:"

Pay attention to the delete and create table check boxes as well, and make sure you examine the generated script before running it.

This is the quickest and most reliable way I've found.

Steven Mays
  • 365
  • 1
  • 12
1

You can use the below C# Method to generate the insert scripts using Excel sheet just you need import OfficeOpenXml Package from NuGet Package Manager before executing the method.

public string GenerateSQLInsertScripts() {

        var outputQuery = new StringBuilder();
        var tableName = "Your Table Name";
        if (file != null)
        {
            var filePath = @"D:\FileName.xsls";

            using (OfficeOpenXml.ExcelPackage xlPackage = new OfficeOpenXml.ExcelPackage(new FileInfo(filePath)))
            {
                var myWorksheet = xlPackage.Workbook.Worksheets.First(); //select the first sheet here
                var totalRows = myWorksheet.Dimension.End.Row;
                var totalColumns = myWorksheet.Dimension.End.Column;

                var columns = new StringBuilder(); //this is your columns

                var columnRows = myWorksheet.Cells[1, 1, 1, totalColumns].Select(c => c.Value == null ? string.Empty : c.Value.ToString());

                columns.Append("INSERT INTO["+ tableName +"] (");
                foreach (var colrow in columnRows)
                {
                    columns.Append("[");
                    columns.Append(colrow);
                    columns.Append("]");
                    columns.Append(",");
                }
                columns.Length--;
                columns.Append(") VALUES (");
                for (int rowNum = 2; rowNum <= totalRows; rowNum++) //selet starting row here
                {
                    var dataRows = myWorksheet.Cells[rowNum, 1, rowNum, totalColumns].Select(c => c.Value == null ? string.Empty : c.Value.ToString());

                    var finalQuery = new StringBuilder(); 
                    finalQuery.Append(columns);

                    foreach (var dataRow in dataRows)
                    {
                        finalQuery.Append("'");
                        finalQuery.Append(dataRow);
                        finalQuery.Append("'");
                        finalQuery.Append(",");
                    }
                    finalQuery.Length--;

                    finalQuery.Append(");");

                    outputQuery.Append(finalQuery);

                  }

            }
        }

return outputQuery.ToString();}
Bhargav Konda
  • 169
  • 1
  • 9
0

Here is a link to an Online automator to convert CSV files to SQL Insert Into statements:

CSV-to-SQL

David Whittaker
  • 671
  • 5
  • 3
0

This query i have generated for inserting the Excel file data into database In this id and price are numeric values and date field as well. This query summarized all the type which I require It may useful to you as well

="insert into  product (product_id,name,date,price) values("&A1&",'" &B1& "','" &C1& "'," &D1& ");"


    Id    Name           Date           price 
    7   Product 7   2017-01-05 15:28:37 200
    8   Product 8   2017-01-05 15:28:37 40
    9   Product 9   2017-01-05 15:32:31 500
    10  Product 10  2017-01-05 15:32:31 30
    11  Product 11  2017-01-05 15:32:31 99
    12  Product 12  2017-01-05 15:32:31 25
vaibhav kulkarni
  • 1,733
  • 14
  • 20
0

I had to make SQL scripts often and add them to source control and send them to DBA. I used this ExcelIntoSQL App from windows store https://www.microsoft.com/store/apps/9NH0W51XXQRM It creates complete script with "CREATE TABLE" and INSERTS.

0

I have a reliable way to generate SQL inserts batly,and you can modify partial parameters in processing.It helps me a lot in my work, for example, copy one hundreds data to database with incompatible structure and fields count. IntellIJ DataGrip , the powerful tool i use. DG can batly receive data from WPS office or MS Excel by column or line. after copying, DG can export data as SQL inserts.

何德福
  • 81
  • 3