Select 来自输出 table 的存储过程的值

Select value from stored procedure which outputs a table

我有一个程序,在一组计算后returns输出一个select语句。执行程序的输出如下:

exec proc1 empID

输出如下

col1 col2 col3 col4 col5
2014 2 33 330 29
2014 3 10 34 12
2015 1 25 60 55

现在我有一个主要的 select 语句,它通过连接不同的表获得许多列。

我需要将上述列(存储过程 proc1 的输出)检索到主 select 语句,以便 empID 可用。

如下所示:

select empID, empName, empSal, (select col3 from [exec proc1 empID] where col2=1), empDept from tblemployee

可以吗?我期望上述查询的第 4 列中有 25 个。

您可以使用 User-defined functionview 来代替存储过程。

sp 不允许 select 在其中使用。

或者你可以

  • 创建一个 table 变量来存储从存储过程返回的结果

Declare @TempTable Table (...)--declare all columns

  • 将存储过程的输出插入table变量,然后

Insert @TempTable Exec storedProcname params

  • 完全按照您的需要加入 Temp table 变量

但是上面的方法有一个局限性 INSERT @Temptable 的问题是

INSERT EXEC statement cannot be nested

。如果您的存储过程中已经有一个 INSERT EXEC,它将中断。