meiwn

统计

常用链接

好的网站

阅读排行榜

评论排行榜

为什么你应该使用SCOPE_IDENTITY()而不是 @@IDENTITY)

Many TSQL books show you how to use @@Identity to get the identity of the most recently added row.  Many
articles online, or in magazines show the same.  What you might not know is that it is potentially a source for some very hard to trace bugs in your application.

@@Identity is potentially a very, very bad thing!  In almost every case, you should use scope_identity() instead.

Why?  @@Identity returns the most recently created identity for your current connection.  When you first use it, it might be fine.  Until someone adds a trigger.  If the trigger causes another identity to be created, guess which identity you'll get in your call to @@Identity?  Not nice.

scope_identity() is much nicer.  It gives you what you're expecting.

posted on 2006-09-18 10:00 努力学习.NET 阅读(87) 评论(1)  编辑 收藏

评论

#1楼 2008-04-24 11:37 23[未注册用户]

你在搞NN蛮,英语要求嘿高  回复 引用