scope_identity vs ident_current

前端 未结 6 1551
深忆病人
深忆病人 2020-12-01 19:02

After much research I am a little confused by which identity tracker I should use in sql.

From what I understand scope_identity will give me the last id updated fro

6条回答
  •  渐次进展
    2020-12-01 19:54

    In that case you need to write the table name, what happens if you decide to change the table name? You then also must not forget to update your code to reflect that. I always use SCOPE_IDENTITY unless I need the ID from the insert that happens in a trigger then I will use @@IDENTITY

    Also the bigger difference is that IDENT_CURRENT will give you the identity from another process that did the insert (in other words last generated identity value from any user) so if you do an insert and then someone does an insert before you do a SELECT IDENT_CURRENT you will get that other person's identity value

    See also 6 Different Ways To Get The Current Identity Value which has some code explaining what happens when you put triggers on the table

提交回复
热议问题