6 Different Ways To Get The Current Identity Value

6 Different Ways To Get The Current Identity Value

From Wiki

http://wiki.lessthandot.com/index.php/6_Different_Ways_To_Get_The_Current_Identity_Value
Jump to: navigation , search

This tip will show you how to get the current identity value from a table and also some things that might act a little different than you would expect.

Let's first create our two simple tables

  1. CREATE TABLE TestOne ( id INT IDENTITY ,SomeDate DATETIME )
  2.     CREATE TABLE TestTwo ( id INT IDENTITY ,TestOneID INT ,SomeDate DATETIME )
  3.  
  4.     --Let's insert 4 rows into the table
  5.     INSERT TestOne VALUES ( GETDATE ( ) )
  6.     INSERT TestOne VALUES ( GETDATE ( ) )
  7.     INSERT TestOne VALUES ( GETDATE ( ) )
  8.     INSERT TestOne VALUES ( GETDATE ( ) )

 

Here are 6 ways to check for the current value

 

Contents [Hide ]

@@IDENTITY

  1. SELECT @@IDENTITY
  2.     --this returns 4

 

 

DBCC CHECKIDENT

  1. DBCC CHECKIDENT ( TestOne, NORESEED)

After running DBCC CHECKIDENT the message returned is Checking identity information: current identity value '4', current column value '4'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

 

MAX function

  1. SELECT MAX ( id)
  2.     FROM TestOne

You can also check with the MAX function but this is not recommended because you might get some other identity value that is not yours but from a different user

 

TOP 1 and ORDER BY DESC

  1. SELECT TOP 1 id
  2.     FROM TestOne
  3.     ORDER BY id DESC

The same applies here as for the max function, this is not recommended

 

IDENT_CURRENT

  1. SELECT IDENT_CURRENT ( 'TestOne' )

 

SCOPE_IDENTITY

  1. SELECT SCOPE_IDENTITY ( )

This one is very similar to @@IDENTITY with one BIG difference (shown later)

 

Let's add a trigger to the TestOne table

  1. CREATE TRIGGER trTestOne ON [ dbo] .[ TestOne]
  2.     FOR INSERT
  3.     AS
  4.     DECLARE @CreditUserID INT
  5.  
  6.     SELECT @CreditUserID = ( SELECT ID FROM Inserted)
  7.  
  8.     INSERT TestTwo VALUES ( @CreditUserID,GETDATE ( ) )
  9.     GO


Let's insert another row into the TestOne table

  1. INSERT TestOne VALUES ( GETDATE ( ) )


Now run this

  1. SELECT @@IDENTITY --1
  2.     SELECT @@IDENTITY ,SCOPE_IDENTITY ( ) ,IDENT_CURRENT ( 'TestOne' ) --1,5,5


Now why doesn't @@IDENTITY return 5 but 1?

T his is because @@IDENTITY doesn't care about scope and returns the last identity value from all the statements , which in this case is from the code within the trigger trTestOne

note : from envykok

Since insert into TestOne will trigger insert into TestTwo, so the last identity is come from TestTwo

 


So the bottom line is this: Always use SCOPE_IDENTITY() unless you DO need the last identity value regradless of scope (for example you need to know the identity from the table insert inside the trigger)


But don't let the above test convince you that SCOPE_IDENTITY() and IDENT_CURRENT('TestOne') will always return the same value. If another user adds a record to TestOne after you inserted but before you run the code for IDENT_CURRENT('TestOne'),the value will be 6 instead of 5 for IDENT_CURRENT('TestOne').

 

This is because it returns the last identity value on the table no matter what user added the record.

note: from envykok

this is because IDENT_CURRENT('TestOne') is any session and any scope , so if other user in other session add record into TestOne before u check IDENT_CURRENT('TestOne') value, then you will not able to get a value what you expected.

SCOPE_IDENTITY()  is current session and the current scope

 

 

  1. --Clean up this mess
  2.     DROP TABLE TestOne,TestTwo

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值