SCOPE_IDENTITY() 与 rs.Fields
这些工作方式有什么区别:
Sql = "INSERT INTO mytable (datapath, analysistime,reporttime, lastcalib,analystname,reportname,batchstate,instrument) " & _
"VALUES (dpath, atime, rtime,lcalib,aname,rname,bstate,instrument) SELECT SCOPE_IDENTITY()"
Set rs = cn.Execute
Set rs = rs.NextRecordset
这个:
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("datapath") = dpath
.Fields("analysistime") = atime
.Fields("reporttime") = rtime
.Fields("lastcalib") = lcalib
.Fields("analystname") = aname
.Fields("reportname") = rname
.Fields("batchstate") = bstate
.Fields("instrument") = instrument
.Update ' stores the new record
id=fields.Fields("rowid") ' ** Answer to Question ***
End With
我的问题具体是这样的:
我处于多用户环境中。用户添加记录后,我需要立即捕获添加的记录的 ROWID。我该怎么做?
这就是我打开记录集的方式:
rs.Open "batchinfo", cn, adOpenKeyset, adLockOptimistic, adCmdTable
what is the difference in the way these work:
Sql = "INSERT INTO mytable (datapath, analysistime,reporttime, lastcalib,analystname,reportname,batchstate,instrument) " & _
"VALUES (dpath, atime, rtime,lcalib,aname,rname,bstate,instrument) SELECT SCOPE_IDENTITY()"
Set rs = cn.Execute
Set rs = rs.NextRecordset
and this:
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("datapath") = dpath
.Fields("analysistime") = atime
.Fields("reporttime") = rtime
.Fields("lastcalib") = lcalib
.Fields("analystname") = aname
.Fields("reportname") = rname
.Fields("batchstate") = bstate
.Fields("instrument") = instrument
.Update ' stores the new record
id=fields.Fields("rowid") ' ** Answer to Question ***
End With
my question is specifically this:
i am in a multiuser environment. immediately after the user adds a record, i need to catch the ROWID of the record added. how do i do this?
this is how i open the recordset:
rs.Open "batchinfo", cn, adOpenKeyset, adLockOptimistic, adCmdTable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不同之处在于添加记录并获取结果的方式。
在第一种情况下,您将发出
INSERT
语句,然后调用SCOPE_IDENTITY
。在第二种情况下,您打开一个可更新游标,向其中添加一条记录并读回新添加的记录。
打开游标可能是一项相当消耗资源的操作(这取决于您如何执行)。它还会降低并发性。
The different is the way you add the record and get the result back.
In the first case, you are issuing an
INSERT
statement followed by a call toSCOPE_IDENTITY
.In the second case, you open an updatable cursor, add a record into it and read the newly added record back.
Opening a cursor may be quite a resource-intensive operation (this depends on how do you do it). It also can degrade concurrency.
您的第一个代码示例在 SQL Server 中不合法。 VALUES 子句后面的名称应该是什么?我想它们应该是参数,但你不能像这样传递参数。是否有某种原因导致您不使用参数化存储过程和参数对象来传递参数?
Your first code example is not legal in SQL Server. What are the names after the VALUES clause supposed to be? I guess they are supposed to be parameters but you cannot pass parameters like that. Is there some reason why you are not using a parameterized stored procedure and parameter objects to pass in parameters?
大多数时候我使用打开记录集、添加数据、更新、获取 ID 方法,但是正如 Quassnoi 所说,这可能会占用大量资源。对于应用程序中被频繁调用或需要尽可能快运行的部分,我倾向于使用以新行 ID 作为返回参数的存储过程。
这是一个代码示例
Most of the time I use the open recordset, add data, update, grab ID method however as Quassnoi said it can be resource intensive. For parts of the application that are called a lot or need to run as fast as possible I tend to use a stored procedure with the new row’s ID as a return parameter.
Here is a code example