SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY not working with Linked Server or Remote server ???
Hello Friends,
I have faced a problem using SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY with Linked server
Let me explain with example as below
Question:
We have set of applications which are running on SQL 2000, due to some reason we need to migrate sub set of applications to SQL 2008.
Now the few applications running on SQL 2008 and few on SQL 2000, applications running on SQL 2008 using some of databases which lie on SQL 2000 with Linked server
Now the problem is the some triggers and stored procedures which need to insert the values generated from SQL 2008 to SQL 2000 databases using functions SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are not working in this scenario.
We are fixing this problem by writing another select statement for selecting identity based on unique parameters in that scope.
Answer:
The scope of the @@IDENTITY function is current session on the local server on which it is executed. This function cannot be applied to remote or linked servers. To obtain an identity value on a different server, execute a stored procedure on that remote or linked server and have that stored procedure (which is executing in the context of the remote or linked server) gather the identity value and return it to the calling connection on the local server.
Same for SCOPE_IDENTITY, IDENT_CURRENT also……
If anybody has any idea, please share as comment…
As mentioned above, fixed this problem by writing another select statement for selecting identity based on unique parameters in that scope.
Temp table may be another solution for this issue (Not tried)
Let me try to explain
As we inserting the records on remote database with Linked server. In the same transaction we can insert the same record to temp table on the same server.
That way we can easily retrieve the unique ID
If anybody has any idea, please share.