sql server:如何获取插入的最后一条记录?

时间:2010-08-19 23:05:09

标签: sql-server sql-server-2008 vba

我这样做:

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
End With

' get the last id
Set rs = cn.Execute("SELECT SCOPE_IDENTITY()", , adCmdText)

这不能正常工作。它返回NULL

2 个答案:

答案 0 :(得分:3)

它不起作用,因为您的更新和第二次执行位于不同的范围内。 您可能需要SELECT IDENT_CURRENT('tablename')

答案 1 :(得分:1)

IDENT_CURRENT适用于单个用户环境。

您在更新时已经记录在案。

.Update
lTheNewID = .Fields("ThisTableID")

lTheNewID将保留新记录的价值。

相关问题