I am attempting to take the results of an EXEC
query and put them into a table in my SQL Server 2017 database.
I'm aware that similar questions have been asked before like here, here, and here, but I have not found a solution that works.
The EXEC
query runs great on its own, and results in exactly the data I am looking for:
-- EXEC master.dbo.sp_serveroption @server=N'OLAP', @optname=N'rpc out', @optvalue=N'true'
DECLARE @sqlcode VARCHAR(MAX)
SET @sqlcode = 'code'
EXEC (@sqlcode) AT OLAP
I cannot use OPENQUERY
due to the SQL exceeding the 8000 character limit.
Attempt 1:
DECLARE @sqlcode VARCHAR(MAX)
SET @sqlcode = 'sqlcode'
DROP TABLE IF EXISTS [jerry].[dbo].[purchases]
truncate table [jerry].[dbo].[purchases]
insert into [jerry].[dbo].[purchases]
exec ( @sqlcode ) at OLAP
But get:
Cannot find the object "purchases" because it does not exist or you do not have permissions.
Attempt 2:
SELECT * INTO [jerry].[dbo].[purchases] FROM OPENROWSET('EXEC (@sqlcode) AT OLAP')
but get an error of
Incorrect syntax near ')'`.
Attempt 3
CREATE TABLE [jerry].[dbo].[purchases] ([Transaction_Date] DATE, [Requirement_Date] DATE, [Element] NVARCHAR(256), [Trx_Quantity] NVARCHAR(256), [Part_Number] NVARCHAR(256), [NHA_Part_Number] NVARCHAR(256), [Group] NVARCHAR(256), [Details] NVARCHAR(256)); INSERT INTO [jerry].[dbo].[purchases] EXEC (@sqlcode) AT OLAP
but get an error of:
OLE DB provider "OraOLEDB.Oracle" for linked server "OLAP" returned message "Unable to enlist in the transaction.". Msg 7391, Level 16, State 2, Line 208 The operation could not be performed because OLE DB provider "OraOLEDB.Oracle" for linked server "OLAP" was unable to begin a distributed transaction.
Attempt 4
DECLARE @sqlcode VARCHAR(MAX)
SET @sqlcode = 'sql'
DROP TABLE IF EXISTS [jerry].[dbo].[report]
CREATE TABLE [jerry].[dbo].[report] ([Transaction_Date] DATE, [Requirement_Date] DATE, [Element] NVARCHAR(256), [Trx_Quantity] NVARCHAR(256), [Part_Number] NVARCHAR(256), [NHA_Part_Number] NVARCHAR(256), [Group] NVARCHAR(256), [Details] NVARCHAR(256));
insert into [jerry].[dbo].[report]
exec ( @sqlcode ) at OLAP
But I get an error of:
Msg 7391, Level 16, State 2, Line 209 The operation could not be performed because OLE DB provider "OraOLEDB.Oracle" for linked server "OLAP" was unable to begin a distributed transaction. And, when I try to change the "Enable Promotion of Distributed Transactions being set to False" per this post, I get another permission's error.
In short, I just need the results of the first EXEC
query entered in to a SQL Server database. The EXEC
query does hit an external Oracle database to which I have ONLY read permissions and cannot change any security settings.
Any assistance is appreciated.