嵌套存储过程

时间:2020-08-22 19:19:40

标签: sql sql-server database stored-procedures

如何将嵌套存储过程的值插入表中。 例如,如果我创建了这样的存储过程。

    Create procedure New
    begin
        create table #tmp
        (
            id int,
            name varchar(50)
        );

        insert into #tmp
            exec stored_procedure 1,2;

        insert into #tmp
            exec stored_procedure 1,2;
        select * from #tmp
    end

现在,如果执行此命令,SQL Server将显示错误:

insert into #table
    exec New;

有人对此问题有解决方案吗?请分享

2 个答案:

答案 0 :(得分:1)

目前,您没有从过程New返回任何数据。如果您希望它返回数据以供您的调用代码插入,则需要在其中插入select语句。

例如在末尾添加一行:

SELECT * FROM #tmp;

此外,您不能嵌套INSERT EXEC语句。有关更多详细信息,请参见this answer

答案 1 :(得分:0)

您不能使用存储过程返回的de值进行插入。

在您的情况下,最简单的方法是通过表函数更改存储过程。 You can find more about table functions here

我的看法是,如果可以的话,请使其变得简单。