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 varchar(max),
@InDelimiter varchar(10)
)
RETURNS
@tblArray TABLE
(
ElementID smallint IDENTITY(1,1),
Element varchar(1000)
)
AS
BEGIN
DECLARE @StrPos smallint,
@StrStart smallint,
@DelimiterLength smallint
SET @DelimiterLength = LEN(@InDelimiter)
WHILE LEN(@InDelimitedString) > 0
BEGIN
--Removed for clarity . do some CHARINDEX manipulation ETc.
END
RETURN
END
Question :
I can't return select
right away , but still , I want to change the fn_split
to inline table valued udf.
How can I do it ?