Re: @@identity



Kerry Moorman wrote:
Bill,

With SQL Server, Scope_Identity is more reliable than @@Identity.

One technique is to batch a Select Scope_Identity() statement with the Insert statement. For a command object named cmd:

cmd.CommandText = "Insert Into ... ; Select Scope_Identity()"

Then, instead of using ExecuteNonQuery, use ExecuteScalar. To retrieve the identity value into an ID variable:

ID = cmd.ExecuteScalar

Kerry Moorman


"Bill Gower" wrote:

I am using a ExecuteNonQuery() to insert a record into a SQL Server database. How do I retrieve the identity value of the row just inserted?

Bill



Another alternative is to use a stored procedure to do the insert, and in that procedure have an output parameter that will populate with the new identity value.

--
http://bytes.thinkersroom.com
.



Relevant Pages

  • Re: @@identity
    ... One technique is to batch a Select Scope_Identitystatement with the ... Then, instead of using ExecuteNonQuery, use ExecuteScalar. ... How do I retrieve the identity value of the row just ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: General Collection assignment question
    ... "Kerry Moorman" wrote: ... You retrieve a Problem object from the combo box and modify it. ... "Michael" wrote: ... with the correct data before the iProblems collection saves the data to the ...
    (microsoft.public.dotnet.languages.vb)