Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Returning Identity key from a linked server

Posted on 2012-12-20
4
Medium Priority
?
1,821 Views
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 ,
          Column2  
         
        )
       OUTPUT INSERTED.MyNewid INTO @newids(id)
        SELECT  Column1 ,
                Column2
               
        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.

Thanks.
0
Comment
Question by:sherbug1015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
Eugene Z earned 2000 total points
ID: 38711404
try to use proc on the remote servrer and get output from proc

see more

http://stackoverflow.com/questions/2621887/sql-server-identity-issue/2621940#2621940
0
 
LVL 25

Expert Comment

by:TempDBA
ID: 38716000
Something like this:

--Create this procedure on link server
CREATE PROCEDURE <ProcedureName>
AS
BEGIN
      DECLARE @newids TABLE (id INT)
      
    INSERT INTO LinkedDB.dbo.LinkedTable
    ( Column1 ,
      Column2  
       
    )
    OUTPUT INSERTED.MyNewid INTO @newids(id)
    SELECT  Column1 ,
                  Column2
    FROM    ReverseLinkedServer.LocalDB.dbo.localtable

    SELECT id from @newids
END      
GO

--On the local server
DECLARE @newids TABLE (id INT)
Insert into @newids
exec LinkedServer.LinkedDB.dbo.<ProcedureName>
0
 
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.
0
 
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.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

688 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