SCOPE_IDENTITY() 与 rs.Fields

发布于 2024-09-15 19:45:48 字数 1024 浏览 7 评论 0原文

这些工作方式有什么区别:

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 技术交流群。

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

发布评论

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

评论(3

治碍 2024-09-22 19:45:48

不同之处在于添加记录并获取结果的方式。

在第一种情况下,您将发出 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 to SCOPE_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.

掀纱窥君容 2024-09-22 19:45:48

您的第一个代码示例在 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?

独自←快乐 2024-09-22 19:45:48

大多数时候我使用打开记录集、添加数据、更新、获取 ID 方法,但是正如 Quassnoi 所说,这可能会占用大量资源。对于应用程序中被频繁调用或需要尽可能快运行的部分,我倾向于使用以新行 ID 作为返回参数的存储过程。

这是一个代码示例

Set cmd = New ADODB.Command

With cmd

    .CommandText = "sptblTest_questions_UPSERT"
    .CommandType = adCmdStoredProc
    .ActiveConnection = dbCon

    .Parameters.Append .CreateParameter("@Question_ID", adInteger, adParamInput, 0, Me.txtQuestion_ID)

    .Parameters.Append .CreateParameter("@Section_ID", adInteger, adParamInput, 0, Me.txtSection_ID)

    .Parameters.Append .CreateParameter("@Question_number", adTinyInt, adParamInput, 0, Me.txtQuestion_number)

    .Parameters.Append .CreateParameter("@Question_text", adVarChar, adParamInput, 1000, Me.txtQuestion_text)

    .Parameters.Append .CreateParameter("@Max_score", adSmallInt, adParamInput, 0, Me.txtMax_score)

    .Parameters.Append .CreateParameter("@User", adVarChar, adParamInput, 50, fOSUserName)

    .Parameters.Append .CreateParameter("@Inserted_ID", adInteger, adParamOutput, 0)

    .Execute

    Me.txtQuestion_ID = .Parameters("@Inserted_ID")

End With

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

Set cmd = New ADODB.Command

With cmd

    .CommandText = "sptblTest_questions_UPSERT"
    .CommandType = adCmdStoredProc
    .ActiveConnection = dbCon

    .Parameters.Append .CreateParameter("@Question_ID", adInteger, adParamInput, 0, Me.txtQuestion_ID)

    .Parameters.Append .CreateParameter("@Section_ID", adInteger, adParamInput, 0, Me.txtSection_ID)

    .Parameters.Append .CreateParameter("@Question_number", adTinyInt, adParamInput, 0, Me.txtQuestion_number)

    .Parameters.Append .CreateParameter("@Question_text", adVarChar, adParamInput, 1000, Me.txtQuestion_text)

    .Parameters.Append .CreateParameter("@Max_score", adSmallInt, adParamInput, 0, Me.txtMax_score)

    .Parameters.Append .CreateParameter("@User", adVarChar, adParamInput, 50, fOSUserName)

    .Parameters.Append .CreateParameter("@Inserted_ID", adInteger, adParamOutput, 0)

    .Execute

    Me.txtQuestion_ID = .Parameters("@Inserted_ID")

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