在Where子句中使用存储过程的结果

时间:2021-11-16 11:03:46

I have a Stored Procedure(SP) in MS SQL 2008 R2 I'm building that requires a list of users to narrow down the data returned.

我正在构建的MS SQL 2008 R2中有一个存储过程(SP),它需要一个用户列表来缩小返回的数据。

The system I'm working on has a GetUsers SP which returns a list of users, and I need to then use this to limit the results returned from the SP I am working on myself.

我正在处理的系统有一个GetUsers SP,它返回一个用户列表,然后我需要使用这个来限制我正在处理的SP返回的结果。

I've had a look at TABLE variables, but not sure if this is quite what I need.

我已经看过了表变量,但不确定这是否是我所需要的。

How would I go about integrating the results of one SP into the where clause of another SP?

如何将一个SP的结果集成到另一个SP的where子句中呢?

2 个解决方案

#1


7  

You can take the results of your SP by

你可以用SP的结果。

DECLARE @yourtablevariable TABLE (fields....)    

INSERT INTO @yourtablevariable
EXEC GetUsers

Then

然后

SELECT *
FROM othertable
    INNER JOIN @yourtablevariable users on othertable.userid=users.userid

Or

SELECT *
FROM othertable
WHERE userid in (SELECT UserID FROM @yourtablevariable)

Alternatively, if possible, you could convert GetUsers to a table valued function.

或者,如果可能的话,您可以将GetUsers转换为一个表值函数。

#2


0  

In SQL Server if the SP returns a TABLE, than the best way to use a table variable (@Table) or memory table (#Table) if the returned amount of data is bigger.

在SQL Server中,如果SP返回一个表,那么使用表变量(@Table)或内存表(# TABLE)的最好方法是,如果返回的数据量更大。

#1


7  

You can take the results of your SP by

你可以用SP的结果。

DECLARE @yourtablevariable TABLE (fields....)    

INSERT INTO @yourtablevariable
EXEC GetUsers

Then

然后

SELECT *
FROM othertable
    INNER JOIN @yourtablevariable users on othertable.userid=users.userid

Or

SELECT *
FROM othertable
WHERE userid in (SELECT UserID FROM @yourtablevariable)

Alternatively, if possible, you could convert GetUsers to a table valued function.

或者,如果可能的话,您可以将GetUsers转换为一个表值函数。

#2


0  

In SQL Server if the SP returns a TABLE, than the best way to use a table variable (@Table) or memory table (#Table) if the returned amount of data is bigger.

在SQL Server中,如果SP返回一个表,那么使用表变量(@Table)或内存表(# TABLE)的最好方法是,如果返回的数据量更大。