Tricks for generating SQL statements in Excel

generate sql insert statements from excel
create sql update script from excel
create sql statement excel
create sql code from excel
create sql query in excel based on cell value
generate insert excel
create table script from excel
excel macro to generate insert statements

Do you have any tricks for generating SQL statements, mainly INSERTs, in Excel for various data import scenarios?

I'm really getting tired of writing formulas with like

="INSERT INTO Table (ID, Name) VALUES (" & C2 & ", '" & D2 & "')"

The semi-colon needs to be inside the last double quote with a closing paren. When adding single quotes around a string, remember to add them outside your selected cell.

(spaces added for visibility - remove before inserting)

=CONCATENATE("insert into table (id, name) values (",C2,",' ",D2," ');")

Here is another view:

=CONCATENATE("insert into table (id, date, price) values (",C3,",'",D3,"',",B3,");")

SQL Queries from Excel: How to generate SQL Insert / Update , use TOAD (even better import feature as it allows you to mix and match columns, and even to import from the clipboard) Function SQLConcat(rng As Range, Optional quoted As Boolean = False, Optional parenthesis As Boolean = False) As String ' ***** ' * Returns a comma separated list for use in SQL IN statements * ' * Params * ' * - rng: Range of cells to concatenate * ' * - quoted: True/False.

Sometimes I use substitute to replace patterns in the SQL command instead of trying to build the sql command out of concatenation. Say the data is in Columns A & B. Insert a top row. In cell C1 place the SQL command using pattern:

insert into table t1 values('<<A>>', '<<B>>')

Then in rows 2 place the excel formula:

=SUBSTITUTE(SUBSTITUTE($C$1, "<<A>>", A2), "<<B>>", B2)

Note the use of absolute cell addressing $C$1 to get the pattern. Especially nice when working with char or varchar and having to mix the single and double quotes in the concatenation. Compare to:

=concatenate("insert into table t1 values '", A2, "', '", B2, "')"

An other thing that has bitten me more than once is trying to use excel to process some chars or varchars that are numeric, except they have leading zeros such as 007. Excel will convert to the number 7.

How to Generate sql insert/delete/update script from excel 2016 , Using excel operator '&' to generate SQL query. Once the data is ready it Excel Pivot Table Tricks to save your time and make you awesome� Press Enter key, to see the string result bellow as we been generate single SQL Statement for insert data into table tbl_user. For multiple SQL Statement you need to add semi column (;) at the end of your statement so it will run insert data line by line.

I used to use String concatenation method to create SQL inserts in Excel. It can work well but can also be a little time consuming and 'fiddly'.

I created an Excel Add-In that makes generating Inserts from Excel easier :

(see the video at the bottom of the page)

How to build multi record INSERT and UPDATE SQL query with MS , This clip , I write insert into script fro excel to run on new query in sql , and next video will Duration: 5:00 Posted: May 11, 2016 Generating all of the SQL was a matter of selecting the first row and dragging down. Any rows from the data worksheet that needed updates had SQL generated, others just got a blank line. At this point it was a matter of copy/pasting the worksheet into SQL Developer to execute the queries.

the VBA approach would be: declare your string and assign your SQL statement like this

dim SqlString as String
SqlString = "SELECT * FROM %1 WHERE (var=%2)"
SqlString = Replace("%1", "Table_Name")
SqlString = Replace("%2", ""value"")

Excel approach is similar, but using the SUBSTITUTE function.

I prefer this approach because it makes the SQL text legible and avoids all the annoying & and concatenate problems. Yes, it takes an extra cell, but its worth it for the audit trail.

Create insert statement in Excel – Excel - Excel Tutorial, The video shows how to use Microsft Excel to prepare / build / construct SQL INSERT and Duration: 13:09 Posted: Jun 24, 2014 One option is to manipulate data in SQL, export it, and build charts in Excel. If you want to skip the export step, though, some SQL programs (like Mode) allow you to build charts on top of your query results. These charts are tied directly to your database, so anytime you rerun a query, your results and visualizations automatically refresh.

I know this pain. I ended up writing about it on my blog...twice. I created a UDF that concatenates a range of cells together with several options. This will always comma separate the values, but will also optionally add single quotes and/or parenthesis as needed.

So, you write the easy part of the sql statement.

VALUES /*string that we don't want to type by hand*/


FROM table
WHERE foo IN (/*another string I don't want to type out*/)

And the custom excel function below will turn values in a spreadsheet range into a nice string for you.

Function SQLConcat(rng As Range, Optional quoted As Boolean = False, Optional parenthesis As Boolean = False) As String
' ***************************************************************
' * Returns a comma separated list for use in SQL IN statements *
' * Params *
' * - rng: Range of cells to concatenate *
' * - quoted: True/False. If true, values are placed inside *
' * of single quotes. Default of false *
' * - parenthesis: Boolean. *
' * Useful for INSERT INTO tbl VALUES(53),(90),(397) *
' * *
' * Author: Christopher J. McClellan *
' * Published under Creative Commons Attribution-Share Alike *
' * *
' * You are free to change, distribute, and pretty much do *
' * whatever you like with the code, but you must give credit *
' * to the original author and publish any derivitive of this *
' * code under the same license. *
' ***************************************************************

Dim tmp As String 'temporary string
Dim row As Long 'first cell is special case
row = 0 'initalize row count
Dim c As Object 'cell
Dim txtwrapperLeft As String, txtwrapperRight As String

If quoted = True And parenthesis = False Then
 txtwrapperLeft = "'"
 txtwrapperRight = "'"
ElseIf quoted = True And parenthesis = True Then
 txtwrapperLeft = "('"
 txtwrapperRight = "')"
ElseIf quoted = False And parenthesis = True Then
 txtwrapperLeft = "("
 txtwrapperRight = ")"
'quoted = false and parenthesis = false
 txtwrapperLeft = ""
 txtwrapperRight = ""
End If

For Each c In rng.Cells
 If row = 0 Then
 tmp = txtwrapperLeft & c.Value & txtwrapperRight
 tmp = tmp & "," & txtwrapperLeft & c.Value & txtwrapperRight
 End If
 row = row + 1
 Debug.Print tmp
Next c

SQLConcat = tmp
End Function

How to use Excel Spreadsheet to generate SQL statements, If you have a lot of data in CSV or Excel file, you may want to insert them into SQL table. In this lesson, I'll teach you how you can do it with a� 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.

Create SQL Insert Statements from a Spreadsheet, Use this method to generate more complex commands involving insert, update and delete. This article explains how to use Excel to generate SQL statements like drop table, insert and update easily Do you have any such small trick? If you have a lot of data in CSV or Excel file, you may want to insert them into SQL table. In this lesson, I’ll teach you how you can do it with a formula. The following example has four columns with data. We want to insert it into an SQL table.

List of SQL commands for Commonly Used Excel Operations, Create SQL Insert Statements from a Spreadsheet I was helping a friend out yesterday who didn't know this little trick so I thought I would share it. There is actually an easy way to create your insert statements using excel. I would like to have macros that will run .sql scripts (that are saved in a folder), instead of having to put the SQL code in the Excel VBA script. I have over 50 scripts that I'd like to automate - and these have many queries in each that are separated by a ; I've tried . db.ExecuteImmediate ("\\c\sql_folder\1.0_Create_My Lovely_Table.sql")

Writing SQL Statements Using Excel Concatenate , Show Unique Values; Write an expression to generate new column; LookUp data from another table; Pivot Table. To perform operations listed� So I tried to create a SQL Query in Oracle to generate these Insert and Update statements for Excel sheet and once the statement will be generated, just paste the statement into Excel sheet cell and your statement is created. You just need to change the table name in the query and run the SQL query into the schema where that table exists.

  • hey guys I am not able to execute the above mentioned formulas on excel 2010 , as it says error in formula. abd i dont know how to format cells(like c2 and d2) for text or number .plz guide me on the same
  • It works fine with any version of Excel. You may need to drop a Text() wrapper around the data. What does the "final" SQL look like?
  • Your approach is spot on. Your code isn't. The last two lines should be: SqlString = Replace(SqlString, "%1", "Table_Name") and SqlString = Replace(SqlString, "%2", """value""")