Not a full bottle on this, but I had the impression @@IDENTITY gives the value of the last identity created in the database no matter where. SCOPE_IDENTITY() gives the value within the scope of the current statement.
I have a table with an identity column. No triggers, just a vanilla table and primary index. I've run this web app code in two separate browser tabs:
Dim connDb As New SqlConnection(myconnectionstring)
Dim transDb As SqlTransaction = connDb.BeginTransaction()
Dim cmdInsert As New SqlCommand("INSERT INTO mytable (somecolumn) VALUES (somevalue)", conn, transDb)
Dim cmdGetIdentity As New SqlCommand("SELECT @@IDENTITY", connDb, transDb)
' Add the record, pause, get identity.
Dim iNewHosp As Int32 = cmdInsert.ExecuteNonQuery() ' Returns 1 correctly.
Threading.Thread.Sleep(5000) ' Give the other tab time to add a record.
Dim iNewID As Int32 = cmdGetIdentity.ExecuteScalar ' Returns the new ID.
' Commit trans, close, dispose, etc..
Note this is all done within a transaction, not sure if that makes a difference. Using SQL Server 2000 here (unfortunately). Tried in SQL2008 as well, same thing happens.
So I run the page in one browser tab then, while it's sleeping, run it again in the other tab, which adds another record. The "problem" I'm seeing is that the first tab then returns the correct ID for its INSERT, as does the other tab. I thought the first tab would return the ID of the most recent insert, ie. the one done in the second tab?
I originally tried using SCOPE_IDENTITY() for this, but it was returning NULL, hence I tried @@IDENTITY just to see what was going on. So I have 2 questions - why would SCOPE_ return NULL, and why does @@IDENTITY return a scope-specific result?