Returning Identity key from a linked server

Posted on 2012-12-20
Last Modified: 2012-12-29
I am doing an insert from one server to another linked server.  I need to have the Identity Key returned to me from the linked server.

My statement looks like this:

DECLARE @newids TABLE (id INT)

INSERT  INTO LinkedServer.LinkedDB.dbo.LinkedTable
        ( Column1 ,
       OUTPUT INSERTED.MyNewid INTO @newids(id)
        SELECT  Column1 ,
        FROM    LocalDB.dbo.localtable

I am getting this error:

A remote table cannot be used as a DML target in a statement which includes an OUTPUT clause or a nested DML statement.

Does anyone know how I can solve this issue.

Question by:sherbug1015
  • 2
LVL 42

Accepted Solution

EugeneZ earned 500 total points
ID: 38711404
try to use proc on the remote servrer and get output from proc

see more
LVL 25

Expert Comment

ID: 38716000
Something like this:

--Create this procedure on link server
      DECLARE @newids TABLE (id INT)
    INSERT INTO LinkedDB.dbo.LinkedTable
    ( Column1 ,
    OUTPUT INSERTED.MyNewid INTO @newids(id)
    SELECT  Column1 ,
    FROM    ReverseLinkedServer.LocalDB.dbo.localtable

    SELECT id from @newids

--On the local server
DECLARE @newids TABLE (id INT)
Insert into @newids
exec LinkedServer.LinkedDB.dbo.<ProcedureName>
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38716110
You may want to double check that.  The only way to return an IDENTITY value is as described by EugeneZ.

This is what SQL Server's BOL has to say on the subject:
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.

And yes I realize you are not explicitly using the @@IDENTITY function, but the same applies.
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38716116
Ah, never mind I see that you are actually following the same approach as EugeneZ suggested.

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now