I have defined the following scalar-valued function:
CREATE OR ALTER FUNCTION getName
(
@name NVARCHAR(200),
@deletedDate DATETIME2(7) = NULL,
@suffix NVARCHAR(50) = NULL
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN QUOTENAME(CONCAT(@name, IIF(@suffix IS NOT NULL, ' ' + @suffix, ''), IIF(@deletedDate IS NOT NULL, CONCAT(' (DELETED - ', FORMAT(@deletedDate, 'dd.MM.yyyy HH:mm:ss'), ')'), '')))
END
And what I'm trying to do is to concatenate its results in a variable as follows:
DECLARE @Columns NVARCHAR(MAX);
SELECT @Columns = COALESCE(@Columns + ',', '') + [dbo].[getName]([Name], [DeletedDate], DEFAULT)
FROM [dbo].[Items]
WHERE [ProjectId] = 1
ORDER BY [Order] DESC;
SELECT @Columns
But the @Columns result only has the last result of the function and if I remove the function and do inline computation it works as expected (having all the values).
I can't understand why is this not working when using the function.