sql-server - Excel VBA 和 ADODB : retrieve auto identity of row inserted into SQL Server from VBA

标签 sql-server excel ado vba

在 Excel 的 VBA 模块中。 使用 ADODB.Connection 和此连接字符串:

"Provider=SQLOLEDB;Data Source=MARS;Initial Catalog=automation;Trusted_connection=yes;"

我想要:

  1. INSERT INTO 测试(数据)VALUES('Something')
  2. 检索新插入行的自动递增标识 (test.data_id)。

最佳答案

Dim identity as integer
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

cn.ConnectionString = "whatever..."
cn.Open
cn.Execute ("INSERT INTO test(data) VALUES ('hello world')")
rs.Open "SELECT @@identity AS NewID", cn
identity = rs.Fields("NewID") 
MsgBox (identity)

关于sql-server - Excel VBA 和 ADODB : retrieve auto identity of row inserted into SQL Server from VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803623/

相关文章:

jquery - 将 html 导出到 Excel,排除某些单元格或列

excel - 使用 excel 宏返回所有 Access (.mdb) 表的列表

vba - 如何从 ADO 查询中检查记录集的所有字段?

sql-server - ADO 与 OLE DB 有什么关系?

sql-server - 无法从时态历史表中删除行

sql-server - sql server计算表中的递归次数

sql-server - 如何处理SQL Server T-SQL try-catch block 中丢失的客户端连接?

c# - 如何在 C# 中捕获 SQLException?

excel - 允许用户复制 Excel 多页用户窗体中的页面

Excel VBA 从超链接列表中提取 Web 数据