I have the following query that is part of a Scalar Variable Function but I get the same result trying to run the query so it appears that the problem is in the concatenation and/or the use of a variable.
DECLARE @useCompTitle BIT
SET @useCompTitle = (SELECT z.useComplianceTitle FROM dbo.tblConfiguration z WHERE z.id = 1)
DECLARE @Output as VARCHAR(MAX) = '';
DECLARE @ReturnValue as VARCHAR(MAX)='';
SELECT @Output = @Output +
'<tr><td style="vertical-align:top">' +
p.ProcessNumber +
'</td><td>' +
(IIF(@useCompTitle = 0, p.Process, p.title)) +
'</td><td style="vertical-align:top">' +
CASE p.recordStatus
WHEN 1 THEN N'Active'
WHEN 2 THEN N'Inactive'
END +
'</td></tr>'
FROM dbo.tblProcess p
CROSS JOIN dbo.Security_Compliance_Record_List(1, 0, 0) AS p1
INNER JOIN dbo.tblERLinkedRecords l ON p.id = l.recID
WHERE l.eventID = 1172
AND l.linkType = 'C'
AND p.id = p1.id
AND p.recordStatus < 3
ORDER BY p.ProcessNumber
PRINT @OutputThe result should return multiple values in @Output but I am only getting the last record in the set.
I have also tried a CASE statement with the same result. If I replace the IIF (or CASE) with a simple field name I get the correct number of values. The CASE for p.RecordStatus works correctly if the IIF is replaced by a simple field value (e.g. p.title). We use the @useCompTitle process in many Stored Procedures but this is the first time in a concatenation query.