Using the result of an Stored procedure in a Select statement

后端 未结 5 553
暗喜
暗喜 2021-01-20 15:27

I have a stored procedure which returns a Dataset(Table). How can I use the result of this stored procedure in a SELECT statement?

I need s

相关标签:
5条回答
  • 2021-01-20 15:38

    I agree with Marcelo mostly, but if you are set on using a stored procedure, or your stored procedure does anything that affects data, you could create a #temp table with the structure of the output of your stored procedure, and then do something like

    INSERT INTO #temp
    EXEC [dbo].[SPGetResults] '900',300,'USD'
    

    And then do your joins and selects on the temp table.

    0 讨论(0)
  • 2021-01-20 15:47

    The answer of Marcelo Cantos is the best one. Also for distributed queries you can use the following script:

    USE [master]
    
    sp_configure 'Ad Hoc Distributed Queries', 1
    RECONFIGURE
    
    USE [YourDB]
    
    SELECT *
    FROM OPENROWSET('SQLNCLI', 'Server=YourServer ;Trusted_Connection=yes;',
        'EXEC YourDB.YourSchema.YourSP ''YourParameters1'', YourParameters2') AS c
    INNER JOIN YourTableOrView ap ON ap.YourPK = c.YourFK
    

    http://www.kodyaz.com/articles/how-to-sql-select-from-stored-procedure-using-openquery-openrowset.aspx

    0 讨论(0)
  • 2021-01-20 15:48

    Create a table-valued user-defined function instead.

    0 讨论(0)
  • 2021-01-20 15:57

    Use Insert Into ... Exec and store the result into a Temp Table... Then you can join the Temp table in your select statement.

    Alternatively as suggested before try converting the SP into a Table valued function.

    This link provides much more options for you... http://www.sommarskog.se/share_data.html

    0 讨论(0)
  • 2021-01-20 16:00

    Here is a simple example of table Value user-defined function :

    create function personSidsByLastName(@lastName varchar(20))
    returns table
    as 
    return 
    select personSid from Person where lastName like @lastName
    
    select * from PersonAddress pa where pa.personSid in (select personSid from personSidsByLastName('ali'))
    
    0 讨论(0)
提交回复
热议问题