Re: @@identity

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Rad,

Why should it be a sproc and can it not dynamicly?

Cor

"Rad [Visual C# MVP]" <rad@xxxxxxxxxx> schreef in bericht
news:%2307vY8ZvHHA.2040@xxxxxxxxxxxxxxxxxxxxxxx
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 Insert statement. ... Then, instead of using ExecuteNonQuery, use ExecuteScalar. ... How do I retrieve the identity value of the row just inserted? ... Doesn't have to be a sproc ... ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: @@identity
    ... Kerry Moorman wrote: ... One technique is to batch a Select Scope_Identitystatement with the Insert statement. ... Then, instead of using ExecuteNonQuery, use ExecuteScalar. ... How do I retrieve the identity value of the row just inserted? ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: bulk insert in C# (?)
    ... Then set the params and call ExecuteNonQuery for each record. ... > Another possible way of doing batch updates is to batch them yourself. ... This is a really fast way of inserting records in ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to return value from forms
    ... One technique is to set a global variable when one of the buttons is ... Another (if you want to retrieve more than just a single value from the ... be sure to close the modal form again after you've retrieved ... > Is there a general technique for returning user selections/results from a ...
    (microsoft.public.access.formscoding)
  • RE: Sql Server SCOPE_IDENTITY()
    ... be called in the same call batch. ... ExecuteNonQuery() and come back and get the identity value. ... You assume all risk for your use. ... © 2006 Microsoft Corporation. ...
    (microsoft.public.dotnet.framework.adonet)