Can I treat the results of a stored procedure like a table?

Deadly 提交于 2019-12-23 20:07:43

问题


Is it possible to do something like this in sql server 2005?

WITH tmpTable AS (EXEC spWhatever)

Or any other way I can query the data returned from the sp? Thanks!!!


回答1:


Temp table:

CREATE TABLE #foo (col1 int, col2 char(10), ...)

INSERT #foo 
EXEC myproc

Or loopback (not sure if this still works). Edit: Could be OPENROWSET as per SQLMenace's answer

SELECT * FROM OPENQUERY (MyServername, 'USE MyDB EXEC myproc')



回答2:


only with a loopback query if you don't first want to create the table, see here: Store The Output Of A Stored Procedure In A Table Without Creating A Table

example

      SELECT * INTO #TempSpWho
            FROM OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;',
    'set fmtonly off exec master.dbo.sp_who')

SELECT * FROM #TempSpWho



回答3:


as far as i know you can not. But you can try using User Defined Functions (UDF) instead of SP, if you do that you can you use it like a table.



来源:https://stackoverflow.com/questions/4630359/can-i-treat-the-results-of-a-stored-procedure-like-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!