Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8518593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:01:19+00:00 2026-06-11T06:01:19+00:00

I want to use a stored procedure to copy a table from my test

  • 0

I want to use a stored procedure to copy a table from my test database to a linked server with the same ID’s / Identity but I can’t get it to work..
I’ve set the IDENTITY_INSERT to ON but it still complains about the ID column.

Here’s my procedure:

CREATE PROCEDURE [dbo].[TEST2PROD_CopyUIDataSServer]
AS Begin
declare @sql nvarchar(max)
-- First truncate target table
set @sql = 'EXEC [LINKEDSERVER].tempdb.sys.sp_sqlexec' + char(39)+ 'TRUNCATE Table [ProductManager].dbo.[UIData]' + char(39)+  ';'
---- SET IDENTITY_INSERT ON
set @sql = @sql + 'EXEC [LINKEDSERVER].tempdb.sys.sp_sqlexec' + char(39)+ 'SET IDENTITY_INSERT [ProductManager].[dbo].[UIData] ON' + char(39)+  ';'
---- INSERT UIDATA records from DB1 into linked server DB2
set @sql = @sql + 'WITH TestData as (SELECT * from ProductManager.dbo.UIData UID)' + NCHAR(13)+  'INSERT INTO   [LINKEDSERVER].[ProductManager].[dbo].[UIData]' + NCHAR(13) + 'select * from TestData;'
print @sql
exec (@sql) 
end

But when I execute the SP it gives me the following error:

The OLE DB provider “SQLNCLI10” for linked server …. could not INSERT INTO table “[LINKEDSERVER].[ProductManager].[dbo].[UIData]” because of column “Id”. The user did not have permission to write to the column.

Linked server properties RPC and RPC out are set to true. I hope someboy can help me out here?

UPDATE: I decided to pull things apart, first I copy the data from the local server to the linked server in a TEMP_TABLE where I don’t have to deal with IDENTITY issues.

Then I wrote a stored procedure on the linked / remote server, since I’m not using SELECT * but specify the column list. Chances are this will work from the local server in an SP too but I don’t have the time or interest to check it out yet..

USE [ProductManager]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[TEST2PROD_CopyBaseTables]
AS BEGIN
DECLARE @DestTable VARCHAR(50)
DECLARE @DestPath VARCHAR(50)
DECLARE @SrceTable VARCHAR(255)
declare @sql nvarchar(max)
DECLARE @columnList varchar(max)
DECLARE @err int

Begin TRY

declare @comma_delimited_list varchar(4000) 
--- FIRST TRY WITH ONE TABLE, EXTENDABLE...
set @comma_delimited_list = 'UIData' 

declare @cursor cursor 
set @cursor = cursor static for  
  select * from dbo.Split(@comma_delimited_list,',') a 

declare @naam varchar(50)
open @cursor 
while 1=1 begin 
  fetch next from @cursor into @DestTable
  if @@fetch_status <> 0 break 

    --Create tablenames
    SET @SrceTable = '[ProductManager].[dbo].TEMP_' + @DestTable
    SET @DestPath = '[ProductManager].[dbo].'+ @DestTable 
    print @srceTable;
    print @DestTable;

    --Truncate target table
    set @sql ='TRUNCATE TABLE '+ @DestPath + ';'

    --Insert statement needs column names
    set @columnList =''
    SELECT  @columnList = coalesce(@columnList + '[' + name + '],','') FROM sys.columns Where OBJECT_NAME(OBJECT_ID) = @DestTable
    if RIGHT(RTRIM(@columnList),1) = ',' 
    begin
        SET @columnList = LEFT(@columnList, LEN(@columnList) - 1) 
    end

    --Transfer data from source table 2 destination
     set @sql = @sql + ' SET IDENTITY_INSERT ' + @DestPath + ' ON;' + ' INSERT INTO ' + @DestPath + '(' + @columnList + ') SELECT ' + @columnList + ' FROM ' + @SrceTable

    print @sql;

    exec (@sql)
end 
-- not strictly necessary w/ cursor variables since the will go out of scope like a normal var 
close @cursor 
deallocate @cursor 

End Try
Begin Catch
 declare @ErrorMsg nvarchar(MAX);
 select @ErrorMsg =  ERROR_MESSAGE();

SELECT @err = @@error IF @err <> 0 Return @err
end Catch
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:01:20+00:00Added an answer on June 11, 2026 at 6:01 am

    IDENTITY_INSERT doesn’t work with linked servers AFAIK, unless you execute dynamic SQL that includes the SET IDENTITY_INSERT in the batch or have some code (Stored Proc for instance) on the remote server which does that for you.

    The IDENTITY_INSERT is per-session (see MSDN) and when you use the remote server this will probably be in a different session from your statement executed via [LINKEDSERVER].tempdb.sys.sp_sqlexec, which causes it to fail as you see it happening.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we return null value from stored procedure. i dont want to use collase
I have a stored procedure written in T-SQL and want to use a table
I simply want to execute a MySQL stored procedure. But I want to use
I have an SQL SERVER database with table, functions, stored procedures. I want to
I want to use a stored procedure that can make some parameters with null
I want to use linq with stored procedure in asp.net, but if it reduces
I want to use CASE in my stored procedure. I am getting some syntax
I have this stored procedure that I want to use for my SAP crystal
I use Entity Framework 4.2 and want to call a stored procedure that has
I have information stored in a database that I want to use to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.