1
我有從我的工作簿中取出數據並將其加載到SQL數據庫中的代碼。它工作得很好,但我只是意識到,一旦我插入了一些內容併爲該行生成主鍵,我想將它添加到我的Excel工作簿中,以便可以針對它們運行更新語句。Excel VBA和MYSQL的Select LAST_INSERT_ID();
在SQL中,我知道我可以運行
Insert into reports (blah, blah, blah, etc...)
Values (1,2,3, etc....)
Select LAST_INSERT_ID();
,並獲得新行我剛插入的主鍵,但我想不出如何獲取該選擇LAST_INSERT_ID的輸出() ;回到Excel中....
我的代碼如下所示:
SQLINSERT = "Insert into `dwInvoice`.`report` (`TrackNumber`, `RegNSB`, `TrackCount`, `TrackNSB`, `TotalBacklog`, `AverageBacklogTrackMargin`, `RegRGP`, `RGPPercent`, `PriceCode`, `PMName`, `PMComments`, `PMMRecommendations`, `Outcome`, `NewGPPercent`, `AdditionalGP`, `HomeBranch`, `HomeBranchCode`, `BranchCode`, `PMM`, `FSRName`, `SMRName`, `DateReport`, `NEDA`, `NEDAName`, `HomeNEDA`, `HomeNEDAName`, `InvCustEB`, `InvCustNum`, `InvCustName`, `InvCustDWIndicator`, `RegNum`, `AppDate`, `InvoiceDate`, `InvoiceNumber`, `PartNumber`, `DWCustEB`, `DWCustNumb`, `DWCustName`, `DWIndicator`, `DRAWstatus`, `MarginType`, `UnitResale`, `UnitCost`, `PriceCost`, `UnitsShipped`, `TeamNum`, `TeamName`, `IndirectCustNum`, `IndirectCustDWCode`, `IndirectCustEB`, `IndirectCustName`) VALUES("
SQLOUTPUT = "Select LAST_INSERT_ID()"
For Each c In Range("A2:A" & lastRow)
SQLBODY = "'" & c.Value & "','" & c.Offset(0, 1).Value & "','" & c.Offset(0, 2).Value & "','" & c.Offset(0, 3).Value & "','" & c.Offset(0, 4).Value & "','" & c.Offset(0, 5).Value & "','" & c.Offset(0, 6).Value & "','" & c.Offset(0, 7).Value & "','" & c.Offset(0, 8).Value & "','" & c.Offset(0, 9).Value & "','" & c.Offset(0, 10).Value & "','" & c.Offset(0, 11).Value & "','" & c.Offset(0, 12).Value & "','" & c.Offset(0, 13).Value & "','" & c.Offset(0, 14).Value & "','" & c.Offset(0, 15).Value & "','" & c.Offset(0, 16).Value & "','" & c.Offset(0, 17).Value & "','" & c.Offset(0, 18).Value & "','" & c.Offset(0, 19).Value & "','" & c.Offset(0, 20).Value & "','" & c.Offset(0, 21).Value & "','" & c.Offset(0, 22).Value & "','" & c.Offset(0, 23).Value & "','" & c.Offset(0, 24).Value & "','" & c.Offset(0, 25).Value & "','" & c.Offset(0, 26).Value & "','" _
& c.Offset(0, 27).Value & "','" & c.Offset(0, 28).Value & "','" & c.Offset(0, 29).Value & "','" & c.Offset(0, 30).Value & "','" & c.Offset(0, 31).Value & "','" & c.Offset(0, 32).Value & "','" & c.Offset(0, 33).Value & "','" & c.Offset(0, 34).Value & "','" & c.Offset(0, 35).Value & "','" & c.Offset(0, 36).Value & "','" & c.Offset(0, 37).Value & "','" & c.Offset(0, 38).Value & "','" & c.Offset(0, 39).Value & "','" & c.Offset(0, 40).Value & "','" & c.Offset(0, 41).Value & "','" & c.Offset(0, 42).Value & "','" & c.Offset(0, 43).Value & "','" & c.Offset(0, 44).Value & "','" & c.Offset(0, 45).Value & "','" & c.Offset(0, 46).Value & "','" & c.Offset(0, 47).Value & "','" & c.Offset(0, 48).Value & "','" & c.Offset(0, 49).Value & "','" & c.Offset(0, 50).Value & "')"
SQLEND = ";"
SQL_GET = "" & SQLIF & SQLINSERT & SQLBODY & SQLEND
cmd.CommandText = SQL_GET
Set rs = cmd.Execute
next c
我知道這是很長...但它的工作原理。
我可以修改改變SQL_GET的聲明:
SQL_GET = "" & SQLIF & SQLINSERT & SQLBODY & SQLEND & SQLOUTPUT & SQLEND
到Select LAST_INSERT_ID() ;
添加到SQL語句的結束,但我仍然不知道如何真正得到該值就是輸出反饋到Excel中。任何想法如何讓這發生?
第一 - 謝謝!我會(嘗試)儘快對此進行測試。現在,我收到以下錯誤: '系統消息:[MySQL] [ODBC 5.3(w)驅動程序] [mysqld-5.6.20]在'Select LAST_INSERT_ID()'附近的SQL語法錯誤第1行' 語法正是我如何將它輸入到實際的SQL查詢中。.... – lordterrin 2014-09-30 18:32:12
哦,從來沒有 - 我只是在原始查詢結束時運行沒有SQLOUTPUT,然後運行你提供的這段代碼。非常感謝! – lordterrin 2014-09-30 18:37:15