存储过程"Insert exec 语句不能嵌套"问题
今天碰到一个问题,存储过程嵌套:存储过程ProcC里嵌套ProcB,ProcB里嵌套ProcA,执行ProcC时就报错“INSERT EXEC 语句不能嵌套”;
下面这个方法可以解决问题:
CREATE PROCEDURE ProcA
AS
SELECT '123456'
GO
--执行成功
EXEC ProcA
CREATE PROCEDURE ProcB
AS
DECLARE @table TABLE(SN nvarchar(500))
INSERT INTO @table
EXEC ProcA
GO
--执行成功
EXEC ProcB
CREATE PROCEDURE ProcC
AS
DECLARE @table TABLE(SN nvarchar(500))
INSERT INTO @table
EXEC ProcB
GO
--执行失败 “INSERT EXEC 语句不能嵌套”
EXEC ProcC
******只需要把ProcC里的insert into @table 改写成 set @Sql='INSERT INTO @table' 就解决了,如果还有存储过程ProcD嵌套ProcC,也只需要ProcD的insert
ALTER PROCEDURE ProcC
AS
DECLARE @table TABLE(SN nvarchar(500))
DECLARE @Sql nvarchar(2000)
SET @Sql='INSERT INTO @table'
EXEC ProcB
GO
--执行成功
EXEC ProcC
SET @Sql='INSERT INTO @table'