我正在尝试创建以下等效项:
DROP TABLE IF EXISTS [jerry].[dbo].[purchases]
SELECT * INTO [jerry].[dbo].[purchases] FROM OPENQUERY(OLAP, '
sql code
');
使用EXEC
(see this question here)
话虽如此,我无法根据multiple sources(and this one)使用SELECT * INTO
I have found some other resources where I can create a new table using EXEC
,但是,我不知道结果表的确切结构(列数,列类型,列名等),因此它必须是动态的。
以下代码可为我提供所需的确切结果表,但是我无法弄清楚如何使用以下查询中的结果数据创建purchases
表:
-- 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
我尝试使用以下内容:SELECT * INTO [jerry].[dbo].[purchases] FROM OPENROWSET('EXEC (@sqlcode) AT OLAP')
,但出现错误 Incorrect syntax near ')'.
我也尝试过(仅供参考):
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
得到一个:
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.
错误。
如果这是一个简单的逻辑问题,我们深表歉意–我觉得我正在竭尽我的研究能力,试图找到一种解决方案,因为我对SQL Server还是很陌生。如果有帮助,我也正在SSMS 2017中工作。
答案 0 :(得分:0)
可以在这篇文章中找到答案: Create new table with results from EXEC
DECLARE @sqlcode VARCHAR(MAX)
SET @sqlcode = 'sqlcode'
truncate table [jerry].[dbo].[purchases]
insert into [jerry].[dbo].[purchases]
exec ( @sqlcode ) at OLAP