当我运行下面的脚本时,我收到错误INSERT EXEC Statement cannot be nested
有些信息会考虑存储过程:
我想创建名为 tmp 的表,而不是将数据插入b,c存储过程中的临时表,而是将数据插入 tmp 表中。要将行映射到特定连接,我可以添加组列,并使用 @@ SPID 填充该列。在存储过程 a 的最后,我将删除特定 @@ SPID 的所有行,以便它可以与其他连接一起使用。 这是一个好的有效的解决方案吗?
谢谢
create procedure a
as
begin try
declare @tbl table(id int)
insert into @tbl
exec b
end try
begin catch
end catch
create procedure b
as
begin try
declare @tbl table(id int)
insert into @tbl
exec c
select * from @tbl
end try
begin catch
end catch
create procedure b
as
begin try
declare @tbl table(id int)
insert into @tbl(id)
values(1)
select * from @tbl
end try
begin catch
end catch
sdfdf
答案 0 :(得分:1)
您可能想阅读Erland Sommarskog的文章How to Share Data Between Stored Procedures
答案 1 :(得分:1)
一般情况下,我会使用特定于会话的临时表,这样就可以减少管理中止事务的开销,因为它可以自行清理。
缺点是它有点脆弱,因为依赖是不可见的:
create procedure a
as
set nocount on
create table #tbl (id int)
exec b
set nocount off
select * from #tbl
go
create procedure b
as
set nocount on
insert into #tbl
values(1)
exec c
go
create procedure c
as
set nocount on
insert into #tbl
values(2)
go
exec a
我通常会回避@@SPID
范围界定,如果你以某种方式忘记清理,你会有意想不到的结果。
那说:你使用的解决方案高度依赖于手头的问题Erland的文章Alex linked指定了所有选项。