在 Excel 的 VBA 模块中。 使用 ADODB.Connection 和此连接字符串:
"Provider=SQLOLEDB;Data Source=MARS;Initial Catalog=automation;Trusted_connection=yes;"
我想要:
- INSERT INTO 测试(数据)VALUES('Something')
- 检索新插入行的自动递增标识 (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/