gpt4 book ai didi

sql-server - 存储过程结果存入 Azure 数据仓库中的临时表

转载 作者:行者123 更新时间:2023-12-05 01:45:42 27 4
gpt4 key购买 nike

Azure数据仓库中,我有一个存储过程,它将返回SELECT命令的结果。

如何将存储过程结果推送到临时表中?

我尝试了以下查询,它返回一条错误消息。

CREATE TABLE #temp
(name varchar(255), created_date datetime)
GO
INSERT INTO #temp
EXEC sp_testproc

输出消息:

Msg 103010, Level 16, State 1, Line 3
Parse error at line: 2, column: 1: Incorrect syntax near 'EXEC'.

最佳答案

Azure SQL 数据仓库不支持 INSERT ... EXEC(按照 here) 。但是,临时表也有不同的范围,这意味着可以在创建它们的存储过程外部查看它们。只需在存储过程中创建临时表,存储过程执行后就可以查看它,例如:

IF OBJECT_ID('dbo.usp_getTableNames') IS NOT NULL DROP PROC dbo.usp_getTableNames;
GO

CREATE PROC dbo.usp_getTableNames
AS

-- Drop table if already exists
IF OBJECT_ID('tempdb..#tables') IS NOT NULL DROP TABLE #tables;

-- Create temp table for viewing outside stored procedure
CREATE TABLE #tables
(
[object_id] INT NOT NULL,
name SYSNAME NOT NULL
)
WITH
(
DISTRIBUTION = HASH([object_id]),
HEAP
);


INSERT INTO #tables
SELECT object_id, name
FROM sys.tables;

GO

-- Run the proc
EXEC dbo.usp_getTableNames;
GO


-- The table table is still available for reading outside the scope of the stored procedure
SELECT *
FROM #tables;

DROP TABLE #tables;
GO

this 的“模块化代码”部分中提供了类似的示例。文章。只是做事的顺序略有不同。

关于sql-server - 存储过程结果存入 Azure 数据仓库中的临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40656393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com