Re: @@identity
- From: "Rad [Visual C# MVP]" <rad@xxxxxxxxxx>
- Date: Tue, 03 Jul 2007 21:22:16 +0300
Kerry Moorman wrote:
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.
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
--
http://bytes.thinkersroom.com
.
- Follow-Ups:
- Re: @@identity
- From: Cor Ligthert [MVP]
- Re: @@identity
- References:
- @@identity
- From: Bill Gower
- @@identity
- Prev by Date: Re: Update DataTable (from csv import) data
- Next by Date: Re: Update DataTable (from csv import) data
- Previous by thread: @@identity
- Next by thread: Re: @@identity
- Index(es):
Relevant Pages
|