SQL Server中INSERT EXEC语句不能嵌套使用(转载)
问:
I have three stored procedures Sp1, Sp2 and Sp3.
The first one (Sp1) will execute the second one (Sp2) and save returned data into #tempTB1 and the second one will execute the third one (Sp3) and save data into #tempTB2.
If I execute the Sp2 it will work and it will return me all my data from the Sp3, but the problem is in the Sp1, when I execute it it will display this error:
INSERT EXEC statement cannot be nested
I tried to change the place of execute Sp2 and it display me another error:
Cannot use the ROLLBACK statement within an INSERT-EXEC statement.
SQL脚本如下:
Create Procedure Sp3 As Begin Select 'Arun' Name, 'Pollachi' Place Union Select 'Vedaraj' Name, 'Devakottai' Place End Go Create Procedure Sp2 As Begin Create Table #tempTB2 ( Name Varchar(50), Place Varchar(50) ) INSERT #tempTB2 Exec Sp3 SELECT 'Sp2' [Source], * FROM #tempTB2 DROP TABLE #tempTB2 End Go Create Procedure Sp1 As Begin Create Table #tempTB1 ( [Source] Varchar(50), Name Varchar(50), Place Varchar(50) ) INSERT #tempTB1 Exec Sp2 select * from #tempTB1 DROP TABLE #tempTB1 End Go Exec Sp1;--报错:An INSERT EXEC statement cannot be nested.
答:
This is a common issue when attempting to 'bubble' up data from a chain of stored procedures. A restriction in SQL Server is you can only have one INSERT-EXEC active at a time. I recommend looking at How to Share Data Between Stored Procedures which is a very thorough article on patterns to work around this type of problem.
For example a work around could be to turn Sp3 into a Table-valued function.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2018-12-04 [SQLServer大对象]——FileTable初体验 (转载)
2015-12-04 ASP.NET MVC的TempData(转载)
2015-12-04 IIS、Asp.net 编译时的临时文件路径(转载)