SQL Server - SELECT FROM存储过程

本文翻译自:SQL Server - SELECT FROM stored procedure

I have a stored procedure that returns rows: 我有一个返回行的存储过程:

CREATE PROCEDURE MyProc
AS
BEGIN
    SELECT * FROM MyTable
END

My actual procedure is a little more complicated, which is why a sproc is necessary. 我的实际程序有点复杂,这就是为什么有必要使用sproc的原因。

Is it possible to select the output by calling this procedure? 是否可以通过调用此过程来选择输出?

Something like: 就像是:

SELECT * FROM (EXEC MyProc) AS TEMP

I need to use SELECT TOP X , ROW_NUMBER , and an additional WHERE clause to page my data, and I don't really want to pass these values as parameters. 我需要使用SELECT TOP XROW_NUMBER和一个额外的WHERE子句来分页我的数据,我真的不想将这些值作为参数传递。


#1楼

参考:https://stackoom.com/question/6GF9/SQL-Server-SELECT-FROM存储过程


#2楼

您要么需要表值函数,要么将EXEC插入临时表:

INSERT INTO #tab EXEC MyProc

#3楼

You can 您可以

  1. create a table variable to hold the result set from the stored proc and then 创建一个表变量来保存存储过程中的结果集然后
  2. insert the output of the stored proc into the table variable, and then 将存储过程的输出插入表变量,然后
  3. use the table variable exactly as you would any other table... 完全像使用任何其他表一样使用表变量...

... sql .... ...... sql ....

Declare @T Table ([column definitions here])
Insert @T Exec storedProcname params 
Select * from @T Where ...

#4楼

It sounds like you might just need to use a view . 听起来你可能只需要使用一个视图 A view allows a query to be represented as a table so it, the view, can be queried. 视图允许将查询表示为表,以便可以查询该视图。


#5楼

You can copy output from sp to temporaty table. 您可以将sp的输出复制到temporaty表。

CREATE TABLE #GetVersionValues
(
    [Index] int,
    [Name]  sysname,
    Internal_value  int,
    Character_Value sysname
)
INSERT #GetVersionValues EXEC master.dbo.xp_msver 'WindowsVersion'
SELECT * FROM #GetVersionValues
drop TABLE #GetVersionValues

#6楼

You can use a User-defined function or a view instead of a procedure. 您可以使用用户定义的函数视图而不是过程。

A procedure can return multiple result sets, each with its own schema. 过程可以返回多个结果集,每个结果集都有自己的模式。 It's not suitable for using in a SELECT statement. 它不适合在SELECT语句中使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值