Excel VBA& MYSQL选择LAST_INSERT_ID();

时间:2014-09-30 18:17:15

标签: mysql excel-vba vba excel

我有从我的工作簿中获取数据并将其加载到SQL数据库中的代码。它运行得很好,但我刚刚意识到,一旦我插入了一些内容并为该行生成了一个主键,我想将它添加回我的excel工作簿中,以便我可以对它们运行更新语句。

在SQL中,我知道我可以运行

Insert into reports (blah, blah, blah, etc...)
Values (1,2,3, etc....)
Select LAST_INSERT_ID();

并获取刚刚插入的新行的主键,但我无法弄清楚如何获取Select 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语句的末尾,但我仍然不知道如何实际获取输出回INTO Excel的值。关于如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:1)

Set rs = cmd.Execute之后提出以下内容:

rs.open SQLOUTPUT

'to dump the recordset into a worksheet range:
Sheet1.Range("A1").CopyFromRecordSet rs

`to dump the result into a variable:
Dim lastInsertID as string
lastInsertID = rs.Fields(1).value