前些天在论坛上看到过一个帖子,
好像完美兄说用下面的方法能巧妙解决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 [email protected]+' and [email protected]+' and [email protected]+'=''否'') and ClassroomType=''1''' --exec(@rsql) set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in ' set @[email protected][email protected] exec sp_executesql @rc,[email protected] int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法 --select @[email protected] --select @cstucount本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx