【问题标题】:Return Table from Store Procedure - SQL Server [duplicate]从存储过程返回表 - SQL Server [重复]
【发布时间】:2018-07-26 11:49:07
【问题描述】:

我正在尝试获取从存储过程返回的表数据。

Create procedure Proc1
as 
begin
Select * from Employee
End
Go

我想把它用作:

Select * from Departments D
inner join (Exec proc1) p
on D.Emp_id = p.Emp_id

请提出建议。

谢谢

【问题讨论】:

  • 虽然你不能让存储过程直接拉出一个表。我猜你想要使用存储过程的原因是传递参数。您实际上可以使用表值函数。

标签: sql sql-server tsql stored-procedures


【解决方案1】:

短版:你不能。存储过程不能用作查询中的数据源。

你能做的最好的是将存储过程的结果放入(临时)表中,然后查询:

create table #sprocResult (
  -- define columns that match the results of the sproc.
  -- You should also define a PK, and possibly other indexes
)

insert into #sprocResult exec proc1

(您也可以使用表值变量。)

【讨论】:

  • 你也可以对变量表做索引
猜你喜欢
  • 2016-11-03
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 2020-12-11
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
相关资源
最近更新 更多