SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problems with INSERT from stored procedure


Problems with INSERT from stored procedure

Author
Message
m.nikiforov
m.nikiforov
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 14
Why insert from stored procedure works well, and start to have error when I've try to do it from over procedure?
It's hard for me to formulate the question, better look at the code:


create procedure test_0 as
declare @t table (ID int)
insert into @t values (1),(2),(3)
select ID from @t
go

create procedure test_1 as
declare @t1 table (ID int)
insert into @t1
exec test_0
select ID from @t1
go

declare @t2 table (ID int)

insert into @t2 -- this works
exec test_0

exec test_1 -- this works

insert into @t2 -- here have error : Msg 8164, Level 16, State 1, Procedure test_1, Line 4 La instrucción INSERT EXEC no se puede anidar.
exec test_1

select ID from @t2
go

drop procedure test_0,test_1
go


if you use temporary tables the result is the same, as with table variables.
bitbucket-25253
bitbucket-25253
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24147 Visits: 25280
Simply put:

INSERT EXEC Statement cannot be nested

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
m.nikiforov
m.nikiforov
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 14
Find the solution. Can do the same using functions:
CREATE  FUNCTION test_0 ()
RETURNS @t table (ID int)
as
begin
insert into @t values (1),(2),(3)
RETURN
end
go

CREATE FUNCTION test_1 ()
RETURNS @t1 table (ID int)
as
begin
insert into @t1 (ID)
(select ID from test_0())
RETURN
end
go

declare @t2 table (ID int)

insert into @t2 (ID)
(select ID from test_0())

insert into @t2 (ID)
(select ID from test_1())

select ID from @t2

drop function test_1,test_0
go


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search