I've been doing a bit of testing with @@identity values, found out some stuff I didn't know that you might find interesting.
---------------
@@IDENTITY
---------------
@@identity returns the most recently generated Identity value, from any table, from YOUR connection. Imagine that we have two tables, both with an Identity column, and we do this:
Insert Into Table1 (EmpName, Title) VALUES('Gil','Mgr')
Insert Into Table2 (Product,Category) VALUES('Milk','Dairy')
In this case, 'Select @@Identity' will return to us the new Identity value from Table2 only, not Table1.
If we needed to know both new Identity values, then we could do something like this:
Insert Into Table1 (EmpName, Title) VALUES('Gil','Mgr')
set @myIden1 = @@Identity
Insert Into Table2 (Product,Category) VALUES('Milk','Dairy')
set @myIden2 = @@Identity
Select @myIden1, @myIden2
Let's say 'Select @@Identity' returns 149 to us. Question: If other users Insert more records into Table2 (generating new Identity values in that table) what will happen? Answer: If we do 'Select @@Identity' again, it will still return 149 to us, because @@Identity returns the most recent Identity value from our connection only, ignoring any other users also inserting new records.
---------------------------------
IDENT_CURRENT('tablename')
---------------------------------
This is a new function available in SQL 2000 (sorry, not version 7.0). You give it a table name, and it returns the last generated Identity value across ALL connections for that specific table (which might be a useful thing to know)
If we do this:
Insert Into Table1 (EmpName, Title) VALUES('Gil','Mgr')
and then do these immediately:
Select @@IDENTITY
Select IDENT_CURRENT('Table1')
then from both commands we will get the Identity value for the new record that we just inserted. But if we wait a few minutes and then again do:
Select @@IDENTITY
Select IDENT_CURRENT('Table1')
then @@IDENTITY will still show the Identity value that we created (since it pertains only to our connection.) But we might get a different value for IDENT_CURRENT: if someone else has inserted a new record into Table1 since we did, then we will now see the value of THEIR new Identity value.
-----------
Ain't that interestin'.
bperry
---------------
@@IDENTITY
---------------
@@identity returns the most recently generated Identity value, from any table, from YOUR connection. Imagine that we have two tables, both with an Identity column, and we do this:
Insert Into Table1 (EmpName, Title) VALUES('Gil','Mgr')
Insert Into Table2 (Product,Category) VALUES('Milk','Dairy')
In this case, 'Select @@Identity' will return to us the new Identity value from Table2 only, not Table1.
If we needed to know both new Identity values, then we could do something like this:
Insert Into Table1 (EmpName, Title) VALUES('Gil','Mgr')
set @myIden1 = @@Identity
Insert Into Table2 (Product,Category) VALUES('Milk','Dairy')
set @myIden2 = @@Identity
Select @myIden1, @myIden2
Let's say 'Select @@Identity' returns 149 to us. Question: If other users Insert more records into Table2 (generating new Identity values in that table) what will happen? Answer: If we do 'Select @@Identity' again, it will still return 149 to us, because @@Identity returns the most recent Identity value from our connection only, ignoring any other users also inserting new records.
---------------------------------
IDENT_CURRENT('tablename')
---------------------------------
This is a new function available in SQL 2000 (sorry, not version 7.0). You give it a table name, and it returns the last generated Identity value across ALL connections for that specific table (which might be a useful thing to know)
If we do this:
Insert Into Table1 (EmpName, Title) VALUES('Gil','Mgr')
and then do these immediately:
Select @@IDENTITY
Select IDENT_CURRENT('Table1')
then from both commands we will get the Identity value for the new record that we just inserted. But if we wait a few minutes and then again do:
Select @@IDENTITY
Select IDENT_CURRENT('Table1')
then @@IDENTITY will still show the Identity value that we created (since it pertains only to our connection.) But we might get a different value for IDENT_CURRENT: if someone else has inserted a new record into Table1 since we did, then we will now see the value of THEIR new Identity value.
-----------
Ain't that interestin'.
bperry