How to pass user-defined table type to inline function
问题 I have some complex function that I want to use in number of queries. It gets some list of values and return aggregate value. For example (I simplify it, it is more complex in deed): CREATE FUNCTION Mean(@N Numbers READONLY) RETURNS TABLE AS RETURN ( SELECT mean = SUM(n.value) / COUNT(*) FROM @N n ) and I want to use it in query: SELECT d.DepartmentName, MeanRate = m.mean FROM Departments d CROSS APPLY Mean( ( SELECT value = e.Rate FROM Employees e WHERE e.DepatmentId = d.DepatmentId ) ) m