"Dan Guzman" <gu******@nospam-online.sbcglobal.net> wrote in message news:<tD*****************@newssvr30.news.prodigy.c om>...
So this is an IDENTITY column? In this case, you can use SCOPE_IDENTITY()
in SQL 2000 to return the last identity value generated on the current
connection. With SQL 7 and earlier, you can use @@IDENTITY but the value
will reflect identity values resulting from trigger inserts as well.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"ree32" <re***@hotmail.com> wrote in message
news:76**************************@posting.google.c om...I am inserting a record into a table that automatically generates
unique ids (i.e. Primary Key). Is there anyway to return this id. As I
am using this on ASP.net page and I really need the ID to update the
page with the new details.
I think on mysql there is something called LAST_INSERT_ID which does
this.
Can you help me - I'm starting to think I'm going insane:
Last year, after spending several days trying to debug a few stored
procedures, we stumbled accross a bug in SQL 7 where @@IDENTITY
returned the wrong value if the table you'd just inserted into had a
self-referencing foreign key.
So I went through the stored procedure generator (code and sp
generator to create our data access layer) and added a whole load of
defensive coding in that detected that it was running on SQL 7 and
"corrected" the return value by subtracting the number of self-ref
foreign keys. This all worked fine for a while, and then stopped
working (being in error on the low side now instead) so we removed
this code, and everything has been fine since.
My only question is, does anyone know when/what corrected this bug,
since I cant seem to find it in any of the patch documents?
As a side note to the OP, who Asked how to use the @@IDENTITY, do the
following:
INSERT INTO tblBlah1 (Col1,Col2,Col3) VALUES (Val1,Val2,Val3);
SELECT @@IDENTITY
(If you want to have it returned in a result set of one row/one
column) or
SET @Variable = @@IDENTITY
(If you want to store the value in a variable)