将存储过程结果插入到临时表中

时间:2019-09-26 00:03:07

标签: sql sql-server stored-procedures temp-tables

我有一个存储过程返回此结果:

enter image description here

我调用存储过程的方式是:

Exec uspGetStandardUsingRoleandPhase '1908003'

我想将这些结果存储到临时表中,因此我使用了insert into,如下所示:

IF OBJECT_ID(N'tempdb.dbo.#tmp', N'U') IS NOT NULL
    DROP TABLE #tmp

CREATE TABLE #tmp
(
    startDate DATE,
    endDate DATE,
    strPhase NVARCHAR(50),
    strBadgeNumber NVARCHAR(30)
)

INSERT INTO #tmp (startDate, endDate, strPhase, strBadgeNumber)
    EXEC uspGetStandardUsingRoleandPhase '1908003'

但是我收到这样的错误:

  

INSERT EXEC失败,因为存储过程更改了目标表的架构。

2 个答案:

答案 0 :(得分:1)

很难说没有看到存储过程的代码,但是我的猜测是该过程还会创建一个名为#tmp的临时表。尝试创建一个具有不同名称的临时表,然后在其中运行INSERT EXEC,或者将代码发布到该过程中,以便我们看到它。

答案 1 :(得分:0)

值得一提的是,如果您使用的是Query Store,这在SQL 2016中也会出现,并且恰好在执行过程期间进行清理。他们建议增加查询存储大小以减少可能性,但除此之外,他们不打算针对SQL 2016对其进行修复(此问题已在2017年修复)

https://support.microsoft.com/en-us/help/4465511/error-556-insert-exec-failed-stored-procedure-altered-table-schema