I have below scalar function:
CREATE FUNCTION [dbo].[GetStringIds]
(
@IdsTbl ids READONLY
)
RETURNS nvarchar(800)
AS
BEGIN
DECLARE @IdsLst nvarchar(800);
SELECT
@IdsLst = COALESCE(@IdsLst + ', ', '') + '(' + CAST(ID1 AS NVARCHAR) + ' : ' + CAST(ID2 AS NVARCHAR) + ')'
FROM
@IdsTbl
Return @IdsLst
END
A table type is passed to this scalar function which contains two fields ID1 and ID2, both int type. This function builds an strings like below:
(12 : 2), (23 : 4), (4 : 34)
NOTE: ID1 can be repeated but not ID2, ID1+ID2 are primary keys. For example:
ID1 | ID2
1 100
1 200
1 110
2 500
3 200
3 600
Now, I am wondering if it is better to convert it to a table valued function in order to increase database performance in terms of execution time.
So in this case, if I convert it to a table valued function, will it increase performance? Is its execution time be reduced? If so how can convert it to a table valued function? I have problems to convert it as I need to declare a variable in which concatenate.