0

First of all, is it even possible ?

I have a stored procedure which looks like:

SELECT this FROM table WHERE this IN (SELECT that FROM another_table WHERE that = @Param)

I would like to replace (SELECT that FROM another_table WHERE that = @Param) by another stored procedure

I am having trouble finding the right syntax to make it work. I tried:

SELECT this FROM table WHERE this IN (EXEC new_stored_procedure @Param)

But this doesn't work. Does somebody know the right syntax to do so ?

Thank you for helping

Pierre Roudaut
  • 1,013
  • 1
  • 18
  • 32

2 Answers2

1

You can create a temporary table

-- match exact columns with datatype returned from the stored procedure
create table #temp(col1 int, col2 .... ) 

insert into #temp(col1,...)
EXEC new_stored_procedure @Param


SELECT this FROM table WHERE this IN (select col from #temp)
Madhivanan
  • 13,470
  • 1
  • 24
  • 29
1

You can use a Table-Valued Function

CREATE FUNCTION [dbo].[Get10Companies]
(   
 @DepartmentId Int
)
RETURNS TABLE 
AS
RETURN 
(
    -- Add the SELECT statement with parameter references here
    SELECT TOP (10) ID from company
            WHERE DepartmentId = @DepartmentId
)


 SELECT * from ( Select * from Get10Companies (1104)) t
jwize
  • 4,230
  • 1
  • 33
  • 51