SQL Server - 在IN语句中调用存储过程

时间:2014-03-27 09:17:47

标签: sql sql-server sql-server-2008 tsql stored-procedures

首先,它甚至可能吗?

我有一个存储过程,如下所示:

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

我想用另一个存储过程替换(SELECT that FROM another_table WHERE that = @Param)

我无法找到合适的语法使其正常工作。我试过了:

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

但这不起作用。有人知道这样做的正确语法吗?

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您可以创建临时表

-- 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)

答案 1 :(得分:1)

您可以使用表格值函数

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