【发布时间】:2010-09-25 01:17:06
【问题描述】:
我想做类似的事情
select * from tvfHello(@param) where @param in (Select ID from Users)
【问题讨论】:
-
Rajivdas:您能否再澄清一下您的问题。恐怕我们每个人都有不同的解读方式
标签: sql-server sql-server-2005 user-defined-functions
我想做类似的事情
select * from tvfHello(@param) where @param in (Select ID from Users)
【问题讨论】:
标签: sql-server sql-server-2005 user-defined-functions
你需要使用CROSS APPLY来实现这个
select
f.*
from
users u
cross apply dbo.tvfHello(u.ID) f
【讨论】:
CROSS APPLY,现在我看到了它的优势,非常感谢@kristof。
AdventureWorks 数据库中的以下作品:
CREATE FUNCTION dbo.EmployeeByID(@employeeID int)
RETURNS TABLE
AS RETURN
(
SELECT * FROM HumanResources.Employee WHERE EmployeeID = @employeeID
)
GO
DECLARE @employeeId int
set @employeeId=10
select * from
EmployeeById(@employeeId)
WHERE @EmployeeId in (SELECT EmployeeId FROM HumanResources.Employee)
根据 Kristof 的专业知识,如果您尝试获取多个值,我已经更新了此示例,例如:
select *
from HumanResources.Employee e
CROSS APPLY EmployeeById(e.EmployeeId)
WHERE e.EmployeeId in (5,6,7,8)
【讨论】:
这对我来说看起来没问题,除了你应该总是在你的函数前面加上它们的模式(通常是 dbo)。所以查询应该是:
SELECT * FROM dbo.tvfHello(@param) WHERE @param IN (SELECT ID FROM Users)
【讨论】: