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.
SQL Server中INSERT EXEC语句不能嵌套使用(转载)的更多相关文章
- SQL Server误区30日谈 第26天 SQL Server中存在真正的“事务嵌套”
误区 #26: SQL Server中存在真正的“事务嵌套”错误 嵌套事务可不会像其语法表现的那样看起来允许事务嵌套.我真不知道为什么有人会这样写代码,我唯一能够想到的就是某个哥们对SQL Serve ...
- SQL Server中的流控制语句
begin···end 该语句定义sql代码块,通常在if和while语句中使用 declare @num int ; ; begin ; print 'hello word' end if···el ...
- SQL Server中使用convert进行日期转换(转载)
一般存入数据库中的时间格式为yyyy-mm-dd hh:mm:ss 如果要转换为yyyy-mm-dd 短日期格式.可以使用convert函数.下面是sqlserver帮助中关于convert函数的声 ...
- Sql server中内连接语句
数据库中学生表和课程表如下: 内连接sql语句: select a.studentName,a.studentAge,b.courseName from student a inner join co ...
- SQL server中的T-SQL语句
首先点击新建查询 如下图所示 创建数据库:create database 数据库名称 使用数据库:use 数据库名称 创建表:create table 表名 ( 代码 ) 输入完成执行时需选中 如果需 ...
- sql server 中使用 LIKE 语句 SqlParameter 使用
原本写的 select * from table where name like '%@searchStr%' 怎么执行都不对,想想 参数是不能加 引号的 于是改为select * from tab ...
- sql server中游标
参考:http://blog.csdn.net/luminji/article/details/5130004 利用SQL Server游标修改数据库中的数据 SQL Server中的UPDATE语句 ...
- SQL Server中【case...end】的用法
在SQL Server中 case...end 语句,一般有如下两种用法: 1.相当于C#中if...else,例: select CName,头衔=case when CLevel='A1' the ...
- SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别
SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别 MSSQL为我们提供了两种动态执行SQL语句的命令,分别是 EXEC 和 SP_EXECUTESQL ,我们先来看一下两种方 ...
随机推荐
- Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之六 || API项目整体搭建 6.1 仓储+服务+抽象接口模式
本文梯子 本文3.0版本文章 前言 零.完成图中的粉色部分 2019-08-30:关于仓储的相关话题 一.创建实体Model数据层 二.设计仓储接口与其实现类 三.设计服务接口与其实现类 四.创建 C ...
- python爬取 “得到” App 电子书信息
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 静觅 崔庆才 PS:如有需要Python学习资料的小伙伴可以加点击下 ...
- vscode 设置代码格式化缩进为2个空格
打开文件——>首选——>设置 输入搜索 tabsize 按照下图设置即可,然后打开 注意:如果不将Detect Indentation 勾选取消 以前用tab创建的忘记依然为4个空格
- Dynamics 365 CE将自定义工作流活动程序集注册到磁盘并引用其他类库
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- mysql 容灾备份
跨服务器备份: 服务器A:192.168.5.193 测试数据库TestDB 服务器B:192.168.5.194 目标:将服务器A上的测试数据库定时备份到服务器B中 需要技术:mysqldump + ...
- 去掉没用的参数的warning
#define UNUSED(x) ((void)(x)) void fun(int noused_arg, int b){ UNUSED(noused_arg); printf("%d\n ...
- gevent实现协程
gevent的好处:能够自动识别程序中的耗时操作,在耗时的时候自动切换到其他任务 # gevent的好处:能够自动识别程序中的耗时操作,在耗时的时候自动切换到其他任务 from gevent impo ...
- 面向对象程序设计(JAVA) 第8周学习指导及要求
2019面向对象程序设计(Java)第8周学习指导及要求 (2019.10.18-2019.10.21) 学习目标 掌握接口定义方法: 掌握实现接口类的定义要求: 掌握实现了接口类的使用要求: 理解 ...
- cidr地址段
CIDR采用各种长度的"网络前缀"来代替分类地址中的网络号和子网号,其格式为:IP地址 = {<网络前缀>,<主机号>}.为了区分网络前缀,通常采用&quo ...
- python的http包
http包简介 http包提供了 HTTP协议的一些功能,主要模块有: http.client --- 底层的 HTTP 协议的一些功能,可以为 urllib.request 模块所用 http. ...