How to query on table returned by Stored procedure within a procedure

后端 未结 3 1446
野性不改
野性不改 2020-12-22 07:45

I have a stored procedure that is performing some ddl dml operations. It retrieves a data after processing data from CTE and cross apply and other such complex things.

相关标签:
3条回答
  • 2020-12-22 08:15

    Edit This wouldn't be suitable for 4 tables but you say in the comments you can convert to return one.

    You can use OPENROWSET for this (Doesn't appear to be mentioned in Paddy's linked question). This requires adhoc distributed queries enabled.

    Example usage

    SELECT  *
    FROM    OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','set fmtonly off exec master.dbo.sp_who')
    AS tbl
    
    0 讨论(0)
  • 2020-12-22 08:15

    I think that your question is similar to this one:

    Access to Result sets from within Stored procedures Transact-SQL SQL Server

    0 讨论(0)
  • 2020-12-22 08:20

    This is not possible.

    The only way I can think of is to insert the results from the Stored Proc in to a temp table and query the temp table. This won't work in your scenario as you are returning 4 tables.

    Could you not rewrite the query you are interested in as a function or view?

    EDIT:

    You would do something along the lines of this (only if the Stored Proc returns one table)

    Create Proc dbo.usp_FetchUsers
    
    as
    
    begin
    
    Select UserId, UserName
    From Users
    
    End
    Go
    
    Create Table #t
    (
    UserId int,
    UserName varchar(50)
    )
    
    Insert Into #t
    Exec dbo.usp_FetchUsers
    
    Select * From #t
    Where UserId = 4
    
    drop table #t
    
    0 讨论(0)
提交回复
热议问题