If I were to use @@IDENTITY
in a SQL sp which also activated a trigger, would it result in a race condition?
Say I perform an INSERT INTO TABLE_A
which triggers an INSERT INTO TABLE_开发者_开发知识库B
then, in the current scope I use @@IDENTITY
Would I always get the resulting identity from the trigger (TABLE_B
) or would it depend on which thread finished first?
Note: I know about SCOPE_IDENTITY
, this question is hypothetical....
I believe you'll always get the identity from table B, because your transaction in Table A isn't considered complete and committed (and your T-SQL isn't allowed to advance) until the insert and trigger are both complete. Even if you're using an AFTER trigger, you're still waiting until the trigger code is done before you get to the next line and ask for the @@identity.
精彩评论