Friday, October 14, 2011

Transfer data from remote server to local

We have a stored procedure on a SQL Server 2005 box which returns us a result-set. We wanted to execute this stored procedure from a SQL Server 2008 box and store the results into a table locally. So, we created a SQL user on the local (2008) box, a linked server to the 2005 box and executed the stored procedure

exec[linkedserver].[db].[schema].[storedproc]
This works fine.

But, if I try to insert this data into a table variable or a temp table, it gives me an error.
DECLARE @temp ASTABLE (column1 datatype, column2 datatype)
INSERT INTO @temp exec [linkedserver].[db].[schema].[storedproc]
The error is

OLE DB provider "SQLNCLI10" for linked server "linkedserver" returned message "The partner transaction manager has disabled its support for remote/network transactions.".
Msg 7391, Level 16, State 2, Line 2
The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "linkedserver" was unable to begin a distributed transaction.

We didnt want to turn on MSDTC for this since there were no distributed transactions involved. So, we decided to use OPENQUERY, like this.
DECLARE @temp ASTABLE (column1 datatype, column2 datatype)
INSERT INTO @temp exec [linkedserver].[db].[schema].[storedproc]
INSERT INTO @temp SELECT * FROM OPENQUERY([linkedserver],'[db].[schema].[storedproc]')

Note that the last argument to OPENQUERY is the command that gets executed on the remote server locally. So, make sure you dont have the linked server prefix in the stored proc's name.

Reference http://msdn.microsoft.com/en-us/library/ms188427.aspx

Also posted this on
http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/2da5983c-2256-4394-82e0-c0c26c663a7e/

No comments:

Post a Comment