I have to call a stored procedure on server B from server A using open query via dynamic SQL, and insert the results into a temp table created on server A. I noticed in the execution plan, it's using a Remote Scan showplan operator. I know when possible the Remote Query showplan operator is more efficient, but I'm not sure if it's possible in this case? (The remote stored procedure returns about 1.5 million rows.)
This is my query on local server A:
CREATE TABLE #FinalResults (Field1 INT, Field2 VARCHAR(100))
DECLARE @DynamicSQL NVARCHAR(MAX) = ''
SELECT @DynamicSQL = @DynamicSQL +
'
SELECT Field1, Field2
FROM OPENQUERY(MyRemoteServerB, ''EXEC sp_MyRemoteStoredProcedure'')
'
INSERT INTO #FinalResults
EXEC sp_ExecuteSQL @DynamicSQL;
This is my query in my remote procedure sp_MyRemoteStoredProcedure on my remote server B:
CREATE TABLE #Results (Field1 INT, Field2 VARCHAR(100))
INSERT INTO #Results
SELECT Field1, Field2
FROM Database1.dbo.Table1
WHERE DateField >= 'Date1'
AND DateField < 'Date2'
UNION
SELECT Field1, Field2
FROM Database2.dbo.Table1
WHERE DateField >= 'Date1'
AND DateField < 'Date2'
SELECT Field1, Field2
FROM #Results