insert exec 不能嵌套
前些天在论坛上看到过一个帖子,
好像完美兄说用下面的方法能巧妙解决insert exec 不能嵌套的问题。
http://topic.csdn.net/u/20091112/20/74132CBF-1570-4673-B12C-2443EA196B06.html
但是我照着做似乎还是不行,再请教一下大家。
- SQL code
if object_id('p1','p') is not nulldrop proc p1gocreate proc p1 ascreate table #t(id int)insert #t select 100select * from #tgoif object_id('p2','p') is not nulldrop proc p2gocreate proc p2 asif object_id('tempdb..#t') is not null insert #t exec p1elsebegin create table #t(id int) insert #t exec p1endselect * from #tgoif object_id('p3','p') is not nulldrop proc p3gocreate proc p3 ascreate table #t(id int)insert #t exec p2select * from #tgoexec p1exec p2exec p3 --INSERT EXEC 语句不能嵌套。[解决办法]
AD
[解决办法]
[解决办法]
试了下
消息 8164,级别 16,状态 1,过程 p2,第 3 行
An INSERT EXEC statement cannot be nested.
[解决办法]
学习
[解决办法]
- SQL code
用临时表的方法, IF object_id('[tempdb].[dbo].#tmp') IS NOT NULL --判断临时表#tmp是否存在,存在则删除 drop table #tmp select * into #tmp from tablename where 1=2 --创建临时表#tmp,其结构与tablename相同 declare @QueryString nvarchar(1000) --动态查询语名变量(注:必须为ntext或nchar哐nvarchar类型,不能是varchar类型) set @QueryString='select * from tablename ' insert into #tmp(field1,field2,...) exec(@querystirng)
[解决办法]
学习。
[解决办法]
[解决办法]
能不能把PRO2改成表函数
[解决办法]
- SQL code
那只能用存储过程调用存储过程了参考一下吧 临时表没办法嵌套 考虑用output来做第一种方法: 使用output参数USE AdventureWorks;GOIF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL DROP PROCEDURE Production.usp_GetList;GOCREATE PROCEDURE Production.usp_GetList @product varchar(40) , @maxprice money , @compareprice money OUTPUT , @listprice money OUTAS SELECT p.name AS Product, p.ListPrice AS 'List Price' FROM Production.Product p JOIN Production.ProductSubcategory s ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.name LIKE @product AND p.ListPrice < @maxprice;-- Populate the output variable @listprice.SET @listprice = (SELECT MAX(p.ListPrice) FROM Production.Product p JOIN Production.ProductSubcategory s ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.name LIKE @product AND p.ListPrice < @maxprice);-- Populate the output variable @compareprice.SET @compareprice = @maxprice;GO另一个存储过程调用的时候:Create Proc TestasDECLARE @compareprice money, @cost money EXECUTE Production.usp_GetList '%Bikes%', 700, @compareprice OUT, @cost OUTPUTIF @cost <= @compareprice BEGIN PRINT 'These products can be purchased for less than $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'ENDELSE PRINT 'The prices for all products in this category exceed $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'第二种方法:创建一个临时表create proc GetUserNameasbegin select 'UserName'endCreate table #tempTable (userName nvarchar(50))insert into #tempTable(userName)exec GetUserNameselect #tempTable--用完之后要把临时表清空drop table #tempTable--需要注意的是,这种方法不能嵌套。例如: procedure a begin ... insert #table exec b end procedure b begin ... insert #table exec c select * from #table end procedure c begin ... select * from sometable end --这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,--会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。第三种方法:声明一个变量,用exec(@sql)执行:1);EXEC 执行SQL语句declare @rsql varchar(250) declare @csql varchar(300) declare @rc nvarchar(500) declare @cstucount int declare @ccount int set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1''' --exec(@rsql) set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in ' set @rc=@csql+@rsql exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法 --select @csql+@rsql --select @cstucount本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx
[解决办法]
怎么不在p2里让结果暂存? 比如用 全域临时表?
- SQL code
if object_id('p1','p') is not nulldrop proc p1gocreate proc p1 ascreate table #t(id int)insert #t select 100select * from #tgoif object_id('p2','p') is not nulldrop proc p2gocreate proc p2 asif object_id('tempdb..#t') is not null insert #t exec p1elsebegin create table #t(id int) insert #t exec p1endif object_id('tempdb..##t') is not null drop table ##tcreate table ##t (id int)insert into ##t select id from #tgoif object_id('p3','p') is not nulldrop proc p3gocreate proc p3 ascreate table #t(id int)insert #t select * from ##tselect * from #tgoexec p1exec p2exec p3 --INSERT EXEC 语句不能嵌套。
[解决办法]
[解决办法]
学习
[解决办法]
learning
[解决办法]
帮顶
[解决办法]
学习
[解决办法]
if object_id('p1','p') is not null
drop proc p1
go
create proc p1 as
create table #t(id int)
insert #t select 100
select * from #t
go
if object_id('p2','p') is not null
drop proc p2
go
create proc p2 as
if object_id('tempdb..#t') is not null
insert #t exec p1
else
begin
create table #t(id int)
insert #t exec p1
end
select * from #t
go
if object_id('p3','p') is not null
drop proc p3
go
create proc p3 as
create table #t(id int)
/*insert #t */exec p2
--select * from #t
go
exec p1
exec p2
exec p3 --INSERT EXEC 语句不能嵌套。
(1 行受影响)
id
-----------
100
(1 行受影响)
(1 行受影响)
id
-----------
100
(1 行受影响)
(1 行受影响)
id
-----------
100
(1 行受影响)
不能嵌套, 不支持 exec..(exec...)
[解决办法]
大概看出了两点:
1.存储过程内insert #t exec 嵌套不能超过两层,超过了就报那个错
2.要分清每个#t不是指的同一个对象,其中有的是直接插入p3中的#t,有的是通过p2,p1中的#t select后插入到p3中的#t
[解决办法]
1.存储过程内insert #t exec 嵌套不能超过两层,超过了就报那个错
是有这个限制的
[解决办法]
所以按30楼的执行应该可以:看看下面的示例应该更清楚
- SQL code
if object_id('p111','p') is not nulldrop proc p111gocreate proc p111 ASselect object_id('tempdb..#t')create table #t(id int)select object_id('tempdb..#t')insert #t select 3select * from #tgoif object_id('p222','p') is not nulldrop proc p222gocreate proc p222 asif object_id('tempdb..#t') is NOT NULLBEGIN select 2 select object_id('tempdb..#t') exec p111END elsebegin create table #t(id int) insert #t exec p111ENDselect object_id('tempdb..#t')select * from #tGOif object_id('p333','p') is not nulldrop proc p333gocreate proc p333 AScreate table #t(id int)select object_id('tempdb..#t')INSERT #t SELECT 1insert #t exec p222select * from #tgoexec p333