定义一个存储过程如下:
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
create
proc
[
dbo
]
.
[
test1
]
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
@id
int
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
as
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
select
1
as
id,
‘
abc
‘
as
name
union
all
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
select
@id
as
id,
‘
zzz
‘
as
name
返回两行数据. 现在想用SQL语句来调用这个存储过程,并把他返回的表放入变量中.可以如下做:
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
declare
@table
table
(id
int
,name
varchar
(
50
))
—
定义表变量来存放存储过程返回的内容
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
insert
into
@table
exec
test1
2
—
将存储过程执行的结果放入表变量中
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
select
*
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
from
@table
—
查看表变量中的结果
![用SQL语句获得一个存储过程返回的表详解数据库](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/4011.html