How to pass parameters to Table Valued Function

后端 未结 3 1095
甜味超标
甜味超标 2021-02-05 04:57

I want to do something like

select * from tvfHello(@param) where @param in (Select ID from Users)
3条回答
  •  隐瞒了意图╮
    2021-02-05 05:21

    The following works in the AdventureWorks database:

    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)
    

    EDIT

    Based on Kristof expertise I have updated this sample if your trying to get multiple values you could for example do:

    select * 
    from HumanResources.Employee e
    CROSS APPLY  EmployeeById(e.EmployeeId)
    WHERE e.EmployeeId in (5,6,7,8)
    

提交回复
热议问题