Tricks for Generating SQL Statements in Excel

Tricks for generating SQL statements in Excel

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,");")

How would I use excel to generate a large update sql statement?

= "update table set name = '" & B1 & "' where namefk = '" & A1 & "'"

where column A1 has name fk & B1 have name
You can drag the formula to achieve query to update thousands of records :)
You can look into the link with the
Example excel
Hope it helps

How to run a SQL query on an Excel table?

There are many fine ways to get this done, which others have already suggestioned. Following along the "get Excel data via SQL track", here are some pointers.

  1. Excel has the "Data Connection Wizard" which allows you to import or link from another data source or even within the very same Excel file.

  2. As part of Microsoft Office (and OS's) are two providers of interest: the old "Microsoft.Jet.OLEDB", and the latest "Microsoft.ACE.OLEDB". Look for them when setting up a connection (such as with the Data Connection Wizard).

  3. Once connected to an Excel workbook, a worksheet or range is the equivalent of a table or view. The table name of a worksheet is the name of the worksheet with a dollar sign ("$") appended to it, and surrounded with square brackets ("[" and "]"); of a range, it is simply the name of the range. To specify an unnamed range of cells as your recordsource, append standard Excel row/column notation to the end of the sheet name in the square brackets.

  4. The native SQL will (more or less be) the SQL of Microsoft Access. (In the past, it was called JET SQL; however Access SQL has evolved, and I believe JET is deprecated old tech.)

  5. Example, reading a worksheet: SELECT * FROM [Sheet1$]

  6. Example, reading a range: SELECT * FROM MyRange

  7. Example, reading an unnamed range of cells: SELECT * FROM [Sheet1$A1:B10]

  8. There are many many many books and web sites available to help you work through the particulars.

Further notes

By default, it is assumed that the first row of your Excel data source contains column headings that can be used as field names. If this is not the case, you must turn this setting off, or your first row of data "disappears" to be used as field names. This is done by adding the optional HDR= setting to the Extended Properties of the connection string. The default, which does not need to be specified, is HDR=Yes. If you do not have column headings, you need to specify HDR=No; the provider names your fields F1, F2, etc.

A caution about specifying worksheets: The provider assumes that your table of data begins with the upper-most, left-most, non-blank cell on the specified worksheet. In other words, your table of data can begin in Row 3, Column C without a problem. However, you cannot, for example, type a worksheet title above and to the left of the data in cell A1.

A caution about specifying ranges: When you specify a worksheet as your recordsource, the provider adds new records below existing records in the worksheet as space allows. When you specify a range (named or unnamed), Jet also adds new records below the existing records in the range as space allows. However, if you requery on the original range, the resulting recordset does not include the newly added records outside the range.

Data types (worth trying) for CREATE TABLE: Short, Long, Single, Double, Currency, DateTime, Bit, Byte, GUID, BigBinary, LongBinary, VarBinary, LongText, VarChar, Decimal.

Connecting to "old tech" Excel (files with the xls extention): Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyFolder\MyWorkbook.xls;Extended Properties=Excel 8.0;. Use the Excel 5.0 source database type for Microsoft Excel 5.0 and 7.0 (95) workbooks and use the Excel 8.0 source database type for Microsoft Excel 8.0 (97), 9.0 (2000) and 10.0 (2002) workbooks.

Connecting to "latest" Excel (files with the xlsx file extension): Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Excel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;"

Treating data as text: IMEX setting treats all data as text. Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Excel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";

(More details at http://www.connectionstrings.com/excel)

More information at http://msdn.microsoft.com/en-US/library/ms141683(v=sql.90).aspx, and at http://support.microsoft.com/kb/316934

Connecting to Excel via ADODB via VBA detailed at http://support.microsoft.com/kb/257819

Microsoft JET 4 details at http://support.microsoft.com/kb/275561

SQL insert using Excel file

=" INSERT INTO Table_Product (TBNID, PRODUCT , PRICE) values ('"&H2&"','"&I2&"','"&J2&"') ;"

just change the Cell locations then drag down.

Excel function to make SQL-like queries on worksheet data?

You can use Get External Data (despite its name), located in the 'Data' tab of Excel 2010, to set up a connection in a workbook to query data from itself. Use From Other Sources From Microsoft Query to connect to Excel

Once set up you can use VBA to manipulate the connection to, among other thing, view and modify the SQL command that drives the query. This query does reference the in memory workbook, so doesn't require a save to refresh the latest data.

Here's a quick Sub to demonstrate accessing the connection objects

Sub DemoConnection()
Dim c As Connections
Dim wb As Workbook
Dim i As Long
Dim strSQL As String

Set wb = ActiveWorkbook
Set c = wb.Connections
For i = 1 To c.Count
' Reresh the data
c(i).Refresh
' view the SQL query
strSQL = c(i).ODBCConnection.CommandText
MsgBox strSQL
Next
End Sub

query SQL in VBA excel

E.g. :


Dim country, city

country = Range("A1").Value
city = Range("B1").Value

Set rs = c.Execute("SELECT * FROM Customers " & _
" WHERE Country='" & country & "'" & _
" AND City='" & city & "'")

Is there a way to use SSIS to execute SQL statements that are stored in an Excel file?

I'm sure it's possible, if a little silly. Why not generate the INSERT statements inside of SSIS?

At any rate, I suppose you'd get an Excel connection manager, use it in an Execute SQL Command task to SELECT the column with the INSERTS from Excel, and output the results to a Recordset. You'd then use a ForEach task to iterate over the recordset, putting each INSERT into a variable. Use the variable to configure an Execute SQL Task inside the ForEach task.

Generate SQL insert statements of some records in a table

I found a way myself using Excel.

  1. Make needed query including WHERE clause in SSMS
  2. Select all the result
  3. Copy with header
  4. Paste in Excel file here under in 4th row, 1st column
  5. Change in macro output path
  6. Change in cell table name
  7. Launch macro
    --> take the file generated and you have a copy of your data ready to be insert again

https://dl.dropboxusercontent.com/u/49776541/GenerateInsert.xlsm

Generating SQL INSERT from data in Excel file

@Al2110

based on your query, To generating SQL insert from data in excel file that show in below:

insert into customers values('" &B3 &"','" &C3& "','"&D3&"');

I hope above information will be useful for you.

Thank you.



Related Topics



Leave a reply



Submit