Returns the final id value generated for any session and specified table in any scope.
Syntax
IDENT_CURRENT('Table_name')
Parameters
Table_name
Is the name of the table whose ID value will be returned.Table_nameThe data type of isVarchar, No default value.
Return type
SQL _variant
Note
IDENT_CURRENT is similar to the SCOPE_IDENTITY and @ IDENTITY functions of Microsoft SQL Server 2000. All three functions return the last generated id value. However, they have different scopes in defining "last" and sessions.
- IDENT_CURRENT returns the last generated id value for any session and specific tables in any scope.
- @ IDENTITY returns the last generated id value for any table in all scopes of the current session.
- SCOPE_IDENTITY returns the last generated id value for the current session and any table in the current scope.
Example
The following example shows the different ID values returned by IDENT_CURRENT, @ IDENTITY, and SCOPE_IDENTITY.
Code
USE pubs
Drop table t6
Drop table t7
GO
Create table t6 (id int IDENTITY)
Create table t7 (id int IDENTITY (100,1 ))
GO
Create trigger t6ins ON t6 FOR INSERT
AS
BEGIN
INSERT t7 DEFAULT VALUES
END
GO
-- End of trigger definition
SELECT * FROM t6
-- Id is empty.
SELECT * FROM t7
-- Id is empty.
-- Do the following in Session 1
INSERT t6 DEFAULT VALUES
SELECT @ IDENTITY
/* Returns the value 100, which was inserted by the trigger .*/
SELECT SCOPE_IDENTITY ()
/* Returns the value 1, which was inserted by
INSERT stmt 2 statements before this query .*/
SELECT IDENT_CURRENT ('t7 ')
/* Returns value inserted into t7, I. e. in the trigger .*/
SELECT IDENT_CURRENT ('t6 ')
/* Returns value inserted into t6, which was the INSERT statement 4 into ts before this query .*/
-- Do the following in Session 2
SELECT @ IDENTITY
/* Returns NULL since there has been no INSERT action
So far in this session .*/
SELECT SCOPE_IDENTITY ()
/* Returns NULL since there has been no INSERT action
So far in this scope in this session .*/
SELECT IDENT_CURRENT ('t7 ')
/* Returns the last value inserted into t7 .*/