您是否有任何技巧在Excel中爲各種數據導入場景生成SQL語句(主要是INSERT)?在Excel中生成SQL語句的技巧
我真的累了寫公式有相似
="INSERT INTO Table (ID, Name) VALUES (" & C2 & ", '" & D2 & "')"
您是否有任何技巧在Excel中爲各種數據導入場景生成SQL語句(主要是INSERT)?在Excel中生成SQL語句的技巧
我真的累了寫公式有相似
="INSERT INTO Table (ID, Name) VALUES (" & C2 & ", '" & D2 & "')"
分號需要與關閉括號的最後一個雙引號內。在字符串周圍添加單引號時,請記住將它們添加到所選單元格之外。
(空間增加了知名度 - 在插入之前先刪除)
=CONCATENATE("insert into table (id, name) values (",C2,",' ",D2," ');")
這裏是另一種觀點認爲:
=CONCATENATE("insert into table (id, date, price) values (",C3,",'",D3,"',",B3,");")
你爲什麼要在Excel中生成SQL?將工作表導出爲CSV文件更簡單快捷,然後使用一些工具將該文件導入SQL數據庫。例如MySQL的LOAD DATA INFILE
聲明。
道歉沒有直接回答你的問題,我知道這不是所有情況下的解決方案。
我昨天在做這件事,是的,這很煩人得到正確的引號。我做的一件事就是擁有一個只包含單引號的命名單元格。鍵入A1 ="'"
(等於,雙引號,單引號,雙引號),然後通過在最低工具欄左側的框中鍵入該單元格來命名該單元格爲「QUOTE」。
有時,構建SQL插入這似乎是最簡單的方法。但是你對此感到厭倦,而且我認爲沒有「聰明」的方法來做到這一點(除了宏或者VBA編程之外)。
我點你,以避免Excel和探索一些其他的想法:
將excel文件導出爲csv可以作爲替代選項(請參閱Bill Krawin的文章 - 作爲新的海報,我還不能添加評論)。不過要注意的是,您可能必須將日期字段的格式更改爲yyyy-mm-dd,否則日期列將全部顯示00/00/00 - 這是因爲MySQL對Microsoft Excel使用了不同的日期格式。或者使用OpenOffice保存csv文件。
如何使用ACE/Jet(a.k.a. Access)SQL查詢和插入Excel工作簿中的數據到源代碼?這需要一個可以是另一個Excel電子表格的ACE/Jet。這裏有一個簡單的例子:
INSERT INTO
[ODBC;Driver={SQL Server};SERVER=MYSERVER;DATABASE=MyDatabase;UID=sa;Pwd=mypassword;].MyTable (ID, Name)
SELECT F1, F2
FROM
[Excel 8.0;HDR=NO;IMEX=1;Database=C:\db.xls;].[Sheet1$A1:B4];
我曾經使用String concatenation方法在Excel中創建SQL插入。它可以很好地工作,但也可能會花費一點時間和「費力」。
我創建了一個Excel加載,使得從Excel中產生的插入更容易:
(見本頁面底部的視頻) http://www.howinexcel.com/2009/06/generating-sql-insert-statements-in-excel.html
有時我使用替換來替換SQL命令中的模式,而不是嘗試從串聯構建sql命令。假設數據在列A & B.插入第一行。在小區C1代替SQL命令,使用圖案:
insert into table t1 values('<<A>>', '<<B>>')
然後,在列2代替Excel公式:
=SUBSTITUTE(SUBSTITUTE($C$1, "<<A>>", A2), "<<B>>", B2)
注使用絕對細胞尋址$C$1
得到圖案。使用char或varchar並且不得不在混合中混合使用單引號和雙引號時尤其好。對比:
=concatenate("insert into table t1 values '", A2, "', '", B2, "')"
已經咬我不止一次試圖使用Excel來處理某些字符或VARCHAR處理是數字,除非他們有前導零,如007 Excel將轉換爲數字的另一件事7.
的VBA的做法是: 聲明你的字符串,並指定你的SQL語句這樣
dim SqlString as String
SqlString = "SELECT * FROM %1 WHERE (var=%2)"
SqlString = Replace("%1", "Table_Name")
SqlString = Replace("%2", ""value"")
Excel的方法是相似的,但使用SUBSTITUTE
功能。
我更喜歡這種方法,因爲它使得SQL文本易於閱讀,避免了所有煩人的問題和連接問題。是的,它需要一個額外的單元,但它的審計線索是值得的。
我知道這種痛苦。我結束了writing about it on my blog ... twice。
我創建了一個UDF,它將一系列單元格與幾個選項連接在一起。這將總是逗號分隔值,但也可以根據需要添加單引號和/或括號。
所以,你寫了sql語句的簡單部分。
INSERT INTO table
VALUES /*string that we don't want to type by hand*/
或
SELECT *
FROM table
WHERE foo IN (/*another string I don't want to type out*/)
而且定義Excel函數的中會變成電子表格中的範圍值到一個不錯的字符串爲您服務。
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 *
' * http://creativecommons.org/licenses/by-sa/3.0/ *
' * 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 = ")"
Else
'quoted = false and parenthesis = false
txtwrapperLeft = ""
txtwrapperRight = ""
End If
For Each c In rng.Cells
If row = 0 Then
tmp = txtwrapperLeft & c.Value & txtwrapperRight
Else
tmp = tmp & "," & txtwrapperLeft & c.Value & txtwrapperRight
End If
row = row + 1
Debug.Print tmp
Next c
'return
SQLConcat = tmp
End Function
嘿傢伙我無法在Excel 2010上執行上述公式,因爲它在公式中表示錯誤。 abd我不知道如何格式化單元格(如c2和d2)的文本或編號.plz指導我在相同的 – 2012-05-29 05:10:56
它適用於任何版本的Excel。您可能需要在數據周圍放置一個Text()包裝器。 「最終」的SQL是什麼樣的? – 2012-06-05 05:25:17