@@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.

 

set @id=scope_identity()

转载于:https://www.cnblogs.com/ami/archive/2006/09/08/499192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值