[英]sql server: is there a way to get last record inserted without stored procedure?
is there a way to get last record inserted without stored procedure in a multi-user environment? 有没有一种方法可以在多用户环境中在没有存储过程的情况下插入最后一条记录?
i am connected to sql server through vba. 我通过VBA连接到SQL Server。 i insert some records and call: 我插入一些记录并致电:
SELECT SCOPE_IDENTITY()
this is not working. 这是行不通的。 it is returning null every time. 每次都返回null。
here is the entire code: 这是完整的代码:
Dim sqlstring1 As String
sqlstring1 = "delete from batchinfo where datapath='" & dpath & "' and analystname='" & aname & "' and reportname='" & rname & "' and batchstate='" & bstate & "'"
Dim rowid_batchinfo As String
rs.Filter = "datapath='" + dpath + "' and analystname='" + aname + "' and reportname='" + rname + "' and batchstate='" + bstate + "'"
If Not rs.EOF Then
rowid_batchinfo = rs.Fields("rowid")
cn.Execute "delete from batchinfo where rowid=" + rowid_batchinfo
cn.Execute "delete from calibration where rowid='" + rowid_batchinfo + "'"
cn.Execute "delete from qvalues where rowid='" + rowid_batchinfo + "'"
End If
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
Set rs = cn.Execute("SELECT SCOPE_IDENTITY()", , adCmdText)
Set rs = rs.NextRecordset
the question is how do i get the last record inserted in a multi-user environment? 问题是如何获取在多用户环境中插入的最后一条记录?
Returns the last identity value inserted into an identity column in the same scope. 返回插入到同一作用域的标识列中的最后一个标识值。 A scope is a module: a stored procedure, trigger, function, or batch. 范围是一个模块:存储过程,触发器,函数或批处理。 Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch. 因此,如果两个语句位于相同的存储过程,函数或批处理中,则它们属于同一范围。
This means that you should send SCOPE_IDENTITY
in the same batch (ie SQL
call) with your insert query. 这意味着您应该与插入查询在同一批(即SQL
调用)中发送SCOPE_IDENTITY
。
This is not possible with methods like AddNew
, so you should use an explicit INSERT
statement followed by SELECT SCOPE_IDENTITY
. 对于类似AddNew
方法,这是不可能的,因此您应该使用显式的INSERT
语句,后跟SELECT SCOPE_IDENTITY
。
Something like this: 像这样:
SQL = "INSERT INTO mytable (datapath, analysistype, …) VALUES (@datapath, @analysistime, …) SELECT SCOPE_IDENTITY()"
Set cmd = Server.CreateOject("ADODB.Command")
With cmd
.CommandType = adCmdText
.CommandText = SQL
.Parameters.Append .CreateParameter("@datapath", adVarWChar, adParamInput,, dpath)
…
End With
Set rs = cmd.Execute
Set rs = rs.NextRecordset
This is what I use in my sp: 这就是我在sp中使用的:
ALTER PROCEDURE [dbo].[usp_app_execSQL_insert] (@SQL AS text)
AS
set nocount on
exec(@sql + ' select scope_identity()')
--select @@identity --works, but returns value from session scope, not insert scope.
--return @@identity --doesn't work
--exec(@sql) return/select scope_identity() -- doesn't work (insert outside scope)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.