使用“SELECT SCOPE_IDENTITY()” 在 ADODB 记录集中

发布于 2024-07-29 08:27:57 字数 1171 浏览 5 评论 0原文

在 Excel 中使用 VBA 脚本,我尝试将新行插入表中,然后获取该行的标识值。 如果我

INSERT INTO DataSheet(databaseUserID, currentTimestamp)
VALUES (1, CURRENT_TIMESTAMP);
SELECT SCOPE_IDENTITY()

在 Management Studio 中运行:,则会插入该行,并按预期提供返回的标识值。 但是,当我通过 VBA 中的 ADODB 记录集运行完全相同的查询时,我遇到了麻烦。 该行确实已插入,但我无法访问标识值。 该记录集列出了 0 个字段,并且实际上也已关闭。 我尝试过使用和不使用分号,并且我还尝试将查询作为单个事务运行。 同样的交易,没有骰子。 知道发生了什么事吗?

这是我的 VBA:

Dim rs As ADODB.Recordset
Dim cn As Connection
Dim SQLStr As String
Dim serverName As String
Dim databaseName As String

serverName = "MSSQLServer"
databaseName = "QA"
cxnStr = "Driver={SQL Server};Server=" & serverName & ";Database=" & databaseName & ";"

SQLStr = "INSERT INTO DataSheet(databaseUserID, currentTimestamp)
VALUES (1, CURRENT_TIMESTAMP); SELECT SCOPE_IDENTITY()"
Set cn = New ADODB.Connection
cn.Open cxnStr
Set rs = New ADODB.Recordset
rs.Open SQLStr, cn, adOpenKeyset, adLockOptimistic
MsgBox (rs.Fields(0).Value)

由于 rs.Fields(0).Value 返回 NULL,因此消息框无法显示。 我向 rs 添加了一个监视,并且正如我所说,在查询后显示 0 个字段,并且似乎已关闭(状态 = 0)。

Using a VBA script in Excel, I'm trying to insert a new row into a table and then get back the identity value of that row. If I run:

INSERT INTO DataSheet(databaseUserID, currentTimestamp)
VALUES (1, CURRENT_TIMESTAMP);
SELECT SCOPE_IDENTITY()

in Management Studio, the row is inserted and it gives me the returned identity value as expected. However, when I run the exact same query through a ADODB recordset in VBA, I'm having trouble. The row is indeed inserted, but I can't access the identity value. The recordset lists 0 fields and has actually been closed as well. I've tried with and without the semicolon, and I also tried running the query as a single transaction as well. Same deal, no dice. Any idea what is going on?

Here's my VBA:

Dim rs As ADODB.Recordset
Dim cn As Connection
Dim SQLStr As String
Dim serverName As String
Dim databaseName As String

serverName = "MSSQLServer"
databaseName = "QA"
cxnStr = "Driver={SQL Server};Server=" & serverName & ";Database=" & databaseName & ";"

SQLStr = "INSERT INTO DataSheet(databaseUserID, currentTimestamp)
VALUES (1, CURRENT_TIMESTAMP); SELECT SCOPE_IDENTITY()"
Set cn = New ADODB.Connection
cn.Open cxnStr
Set rs = New ADODB.Recordset
rs.Open SQLStr, cn, adOpenKeyset, adLockOptimistic
MsgBox (rs.Fields(0).Value)

And the message box fails to display because the rs.Fields(0).Value returns NULL. I added a watch to rs, and, like I said, shows 0 fields after the query and also appears to be closed (state = 0).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月寒剑心 2024-08-05 08:27:58

看看当您删除 adOpenKeySet 和 adLockOptimistic 值并将其保留为默认值时会发生什么。

See what happens when you remove the adOpenKeySet and adLockOptimistic values leave them at their defaults.

疯到世界奔溃 2024-08-05 08:27:57

当您使用 ADODB 运行一批命令时,我相信它会单独运行每个命令。 要强制运行下一个命令,您必须使用以下命令:

Set rs = rs.NextRecordset()

将例程的结尾更改为以下内容应该可以解决问题:

Set rs = New ADODB.Recordset
rs.Open SQLStr, cn, adOpenKeyset, adLockOptimistic
Set rs = rs.NextRecordset
MsgBox (rs.Fields(0).Value)

When you run a batch of commands using ADODB, I believe it runs each one seperately. To force the next command to run, you have to use the following:

Set rs = rs.NextRecordset()

Changing the end of your routine to the following should do the trick:

Set rs = New ADODB.Recordset
rs.Open SQLStr, cn, adOpenKeyset, adLockOptimistic
Set rs = rs.NextRecordset
MsgBox (rs.Fields(0).Value)
千纸鹤带着心事 2024-08-05 08:27:57

您正在执行两条语句,因此您将得到两个结果。 Recordset 对象一次只能保存一个结果 - 要获取其他结果,您需要使用 NextRecordset 方法。

Set rs = rs.NextRecordset

You are executing two statements so you will get two results back. the recordset object can only hold one result at a time - to get the other result you need to use the NextRecordset method.

Set rs = rs.NextRecordset
只怪假的太真实 2024-08-05 08:27:57

在你的 rs.Open 中试试这个

rs.Open SQLStr, cn, adCmdText

In your rs.Open Try this

rs.Open SQLStr, cn, adCmdText

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文