问:


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.

原文链接

SQL Server中INSERT EXEC语句不能嵌套使用(转载)的更多相关文章

  1. SQL Server误区30日谈 第26天 SQL Server中存在真正的“事务嵌套”

    误区 #26: SQL Server中存在真正的“事务嵌套”错误 嵌套事务可不会像其语法表现的那样看起来允许事务嵌套.我真不知道为什么有人会这样写代码,我唯一能够想到的就是某个哥们对SQL Serve ...

  2. SQL Server中的流控制语句

    begin···end 该语句定义sql代码块,通常在if和while语句中使用 declare @num int ; ; begin ; print 'hello word' end if···el ...

  3. SQL Server中使用convert进行日期转换(转载)

    一般存入数据库中的时间格式为yyyy-mm-dd hh:mm:ss 如果要转换为yyyy-mm-dd  短日期格式.可以使用convert函数.下面是sqlserver帮助中关于convert函数的声 ...

  4. Sql server中内连接语句

    数据库中学生表和课程表如下: 内连接sql语句: select a.studentName,a.studentAge,b.courseName from student a inner join co ...

  5. SQL server中的T-SQL语句

    首先点击新建查询 如下图所示 创建数据库:create database 数据库名称 使用数据库:use 数据库名称 创建表:create table 表名 ( 代码 ) 输入完成执行时需选中 如果需 ...

  6. sql server 中使用 LIKE 语句 SqlParameter 使用

    原本写的 select * from table where name like  '%@searchStr%' 怎么执行都不对,想想 参数是不能加 引号的 于是改为select * from tab ...

  7. sql server中游标

    参考:http://blog.csdn.net/luminji/article/details/5130004 利用SQL Server游标修改数据库中的数据 SQL Server中的UPDATE语句 ...

  8. SQL Server中【case...end】的用法

    在SQL Server中 case...end 语句,一般有如下两种用法: 1.相当于C#中if...else,例: select CName,头衔=case when CLevel='A1' the ...

  9. SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别

    SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别 MSSQL为我们提供了两种动态执行SQL语句的命令,分别是 EXEC 和 SP_EXECUTESQL ,我们先来看一下两种方 ...

随机推荐

  1. C# 矢量图EMF 总结

    个人知识记录.如果有用请点赞,否则勿喷.忽略. 个人站点:https://i.cnblogs.com/EditPosts.aspx?opt=1 注意:句柄的操作1.创建 代码如下: Metafile ...

  2. Java自定义注解(1)

    Java注解简介 1. Java注解(Annotation) Java注解是附加在代码中的一些元信息,用于一些工具在编译. 运行时进行解析和使用,起到说明.配置的功能. 注解相关类都包含在java.l ...

  3. PlayJava Day008

    今日所学: /* 2019.08.19开始学习,此为补档. */ 1.包装类:对基本数据类型进行封装,使其具有属性和方法. Integer s1 = new Integer(123) ; Intege ...

  4. File文件的创建,删除 createNewFile() delete()

    package seday03; import java.io.File;import java.io.IOException; /*** 使用File新建一个test1.txt文件* @author ...

  5. linux tmux用法

    1. 安装工具 Centos : yum install tmux 2. 基本操作 新建会话:tmux new -s session-name 查看会话:tmux ls 进入会话:tmux a -t  ...

  6. Java生鲜电商平台-商品分类表和商品类型表的区别与数据库设计

    Java生鲜电商平台-商品分类表和商品类型表的区别与数据库设计   二者服务的对象不一样 目的也是不一样的 商品分类是为商品服务的 用来管理商品 商品类型是为扩展属性服务的 用来管理属性 举例:[转] ...

  7. Windows Store可以下载安装Windows Terminal (Preview)

    Windows Terminal (Preview)已经可以在Windows Store下载安装. Windows Terminal (Preview)运行要求为: Windows 10 版本 183 ...

  8. 2-2-for循环

    重复执行某些代码 每次执行的时候有个数字在变化 常用格式 <script> for(var i=0; i<3; i++){alert(i); } </script> 1) ...

  9. netcore之RabbitMQ入门

    简介 RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息中间件,基于Erlang语言编写. AMQP是什么 AMQP 0-9-1(高级消息队列协议)是一种消息传递协议,它允许一致的客户端应 ...

  10. [20190515]热备份模式与rman冲突.txt

    [20190515]热备份模式与rman冲突.txt --//别人的系统做dg时打开热备份模式,忘记关闭,做rman备份时报错.做一个记录.--//实际上也怪自己,实施时没有讲清楚.通过例子说明: 1 ...