【存储过程】用SQL语句获得一个存储过程返回的表

定义一个存储过程如下:

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    --查看表变量中的结果

 

这么写的话,如果列很多,将会很麻烦

目前没有一个类似 as temp表的方法

select * from (exec test1 1) as temp

后续再找找看吧

posted @ 2018-03-27 11:06  李0539  阅读(714)  评论(0编辑  收藏  举报