What your your reasons for choosing to use a WHILE
loop here, and a multi-line scalar function? Inline Table-value functions are extremely powerful, and quick (as in inline scalar functions in SQL Server 2019).
As for the WHILE
, why not use a set-based method?
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk