This question already has an answer here:
- Errors: “INSERT EXEC statement cannot be nested.” and “Cannot use the ROLLBACK statement within an INSERT-EXEC statement.” How to solve this? 10 answers
I have three Procedures MainProcedure,Procedure1,Procedure2
1) In Procedure1 I just have a select statement ,
2) In Procedure2 am calling the Procedure1 and inserting the Output to a #table
3) In the main Procedure I am calling the Procedure2 and iam trying to insert the Output to a #table which throws an error
Msg 8164, Level 16, State 1, Procedure Procedure2, Line 10 An INSERT EXEC statement cannot be nested.
I can resolve this using Openrowset where I need to use specify Server Name ,is there any other way to solve this by not specifying the servername details
please find the sample procedure for reference
Create Proc Procedure1
As
Begin
Select 'Arun' Name, 'Pollachi' Place
Union
Select 'Vedaraj' Name, 'Devakottai' Place
End
Go
Create Proc Procedure2
As
Begin
Create Table #Table1
(
Name Varchar(50), Place Varchar(50)
)
INSERT #Table1
Exec Procedure1
SELECT 'Procedure2' [Source], * FROM #Table1
DROP TABLE #Table1
End
Go
Create Proc MainProcedure
As
Begin
Create Table #Table1
(
[Source] Varchar(50), Name Varchar(50), Place Varchar(50)
)
INSERT #Table1
Exec Procedure2
select * from #Table1
DROP TABLE #Table1
End
Go
can any one change my main procedure and make it to get executed Thanks!!