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

时间:2016-11-17 13:37:40

标签: sql-server azure stored-procedures azure-sqldw

Azure Data Warehouse中,我有Stored Procedure,它将返回SELECT命令的结果。

如何将Stored Procedure结果推送到临时表?

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

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'.

1 个答案:

答案 0 :(得分:3)

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

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文章的“模块化代码”部分提供了类似的示例。这只是一个略微不同的做事顺序。

相关问题