Insert怎样返回该条纪录的主键(自动编号)的值?

如果使用Insert 再 Select 的做法,并发上不太好处理,所以想知道是否有其他办法?

---------------------------------------------------------------
对SQL Server

select @@IDENTITY

@@IDENTITY 全局变量,记录最近一次insert或updata的id。

详情可以参考SQL帮助。
---------------------------------------------------------------

你可以在进行数据库操作的时候锁定数据库,不许其他用户修改,然后你取出最新ID之后再解锁
---------------------------------------------------------------

' Open our table, it's important to use the Keyset cursor if
' you're using ODBC to connect to your data source, if you're
' using OLEDB then you can also use a Dynamic cursor, and
' an Optimistic or Pessimistic Lock, however a Keyset cursor
' with an Optimistic lock in the most resource efficient..
objRst.Open "BurgerChainOwners", objCnn, adOpenKeyset, _
adLockOptimistic, adCmdTable

' Call the AddNew method, which moves the current row
' pointer on a new row.
objRst.AddNew

' Now, set the values for the fields in that row.
objRst("f_name") = "Ronald"
objRst("s_name") = "McDonald"
objRst("company") = "McDonalds Chain of Restaurants"
objRst("fav_burger") = "Cheeseburger"

' Commit the changes by calling Update.
objRst.Update

' Get the value of the record that we just inserted.
Response.Write objRst("id")

Published At
Categories with Web编程
Tagged with
comments powered by Disqus