inline-table-function

How to pass user-defined table type to inline function

谁说胖子不能爱 提交于 2020-01-07 04:36:10
问题 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

How can I convert split function to inline table valued udf in SQL server?

怎甘沉沦 提交于 2019-11-28 13:05:58
Assuming I have this query ( pseudo) : Select T.a, T.b, (select top 1 element from fn_split(c,',') where element=T.element) From largeTable T Where fn_split runs for each row , I would like to use inline table valued udf so , that performance will be better. NB : fn_split just create a table via splitting via , : But looking at inline table valued udf structure : create FUNCTION [dbo].[fn_...] ( ... ) RETURNS table AS RETURN SELECT ...(!!!) It should return the select right away as the first statement ! But what if my UDF looks like : CREATE FUNCTION [dbo].[FN_Split] ( @InDelimitedString

How can I convert split function to inline table valued udf in SQL server?

怎甘沉沦 提交于 2019-11-27 07:28:57
问题 Assuming I have this query ( pseudo) : Select T.a, T.b, (select top 1 element from fn_split(c,',') where element=T.element) From largeTable T Where fn_split runs for each row , I would like to use inline table valued udf so , that performance will be better. NB : fn_split just create a table via splitting via , : But looking at inline table valued udf structure : create FUNCTION [dbo].[fn_...] ( ... ) RETURNS table AS RETURN SELECT ...(!!!) It should return the select right away as the first