将存储过程执行后的返回值插入到表中。

insert into 表名
exec 存储过程

但是当前存储过程内容中,如果也存在将其他存储过程的返回值插入到表中这种方式,则SQL Server就会报错。

create table Student(
UserId int,
UserName nvarchar(20),
Age int
)
go
create table Stu(
UserId int,
UserName nvarchar(20),
Age int
)
go
insert Student
values(1,'張三',25),(2,'李四',28),(3,'王五',30)
go

create procedure proc_one(
@userId int
)
as
begin
select * from Student where UserId=@userId
end
go

create procedure proc_two(
@userId int
)
as 
begin
insert Student
exec proc_one 1
select * from Student where UserId=@userId
end
go

insert Stu
exec proc_two 2

error:An INSERT EXEC statement cannot be nested.

解决方法:

1,改变原有存储过程

2,将当前存储过程改为函数实现

3,采用openrowset和linked server方式解决

 

 posted on 2018-06-22 18:48  会飞的金鱼  阅读(1764)  评论(0)    收藏  举报