日期:2014-05-18  浏览次数:20446 次

怎么接收存储过程的返回值
我有一个存储过程是这样写的:
  Create procedure GetDetail_sp

as 
  begin
  select t1.lastname,
  t1.firstname,
  t1.country,
  t1.state,
  t1.city
  from table t1
  union 
  select t2.lastname,
  t2.firstname,
  t2.country,
  t2.state,
  t2.city
  from #Specified t2  
  end 

这样执行存储过程后,实际上返回的就是个table,我怎么能接收这个table呢?
比如: exec GetDetail_sp
接着我想查出某个lastname的信息,select * from ?, 这个地方该怎么写?


------解决方案--------------------
楼主事先要知道存储过程返回结果集都有什么字段,然后创建个临时表

create table #temp(...字段...)

insert into #temp exec 过程名 参数

select ... from #temp

drop table #temp

试试。
------解决方案--------------------
可以使用表值函数
create FUNCTION [dbo].[f_GetTable]()
RETURNS TABLE
AS
RETURN(
select 1 Id,'张三' name
union all select 2 Id,'李四' name
)

查询
select * from [dbo].[f_GetTable]()
------解决方案--------------------
用表值函数