【问题标题】:VBA How to find last insert id?VBA如何找到最后一个插入ID?
【发布时间】:2010-03-21 19:22:40
【问题描述】:

我有这个代码:

With shtControleblad
    Dim strsql_basis As String
        strsql_basis = "INSERT INTO is_calculatie (offerte_id) VALUES ('" & Sheets("controleblad").Range("D1").Value & "')"

        rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic

        Dim last_id As String
        last_id = "select last_insert_id()"
End With

字符串 last_id 未填充。怎么了?我需要找到 te last_insert_id 以便在其他查询中使用它。

【问题讨论】:

  • last_id 不包含“select last_insert_id()”?你赌多少钱?

标签: mysql excel vba lastinsertid


【解决方案1】:

您必须在打开记录集后添加rs.movelast,这应该会有所帮助

【讨论】:

  • 这将如何适用于我的情况?那么当更多人使用数据库时呢?
  • 您真正想要做什么?您想根据插入语句选择最后插入的 id?也许您应该先运行一条 select 语句,然后移动到最后一条记录,从中读取,然后使用该数据执行 insert 语句。
【解决方案2】:
last_id = "select last_insert_id()"

您已设置要执行的 sql 语句,但 没有执行它
使用上述语句调用 rs.Open 以获取“last_insert_id”。

如果mysql支持单行多条sql语句,你可以这样做

strsql_basis = "INSERT INTO is_calculatie (offerte_id)  
VALUES ('" & Sheets("controleblad").Range("D1").Value & "')
; select last_insert_id()"

rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic

【讨论】:

  • 不支持多语句。我试过 Dim last_id As String last_id = "select last_insert_id()" rs.Open last_id, oConn, adOpenDynamic, adLockOptimistic 但不是想要的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2011-04-30
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多