定义一个存储过程如下:

create
proc
[
dbo
]
.
[
test1
]

@id
int

as

select
1
as
id,
‘
abc
‘
as
name
union
all

select
@id
as
id,
‘
zzz
‘
as
name
返回两行数据. 现在想用SQL语句来调用这个存储过程,并把他返回的表放入变量中.可以如下做:

declare
@table
table
(id
int
,name
varchar
(
50
))
—
定义表变量来存放存储过程返回的内容

insert
into
@table
exec
test1
2
—
将存储过程执行的结果放入表变量中

select
*

from
@table
—
查看表变量中的结果

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/database/4011.html