No puede depender de la concatenación como:
SELECT @result = COALESCE(@result + ',', '') + [Title]
FROM Episodes
...
Ejemplo dependiendo de CTE/tabla temporal/plan de ejecución, obtendrá diferentes resultados:
DECLARE @text VARCHAR(MAX) = ''
,@text2 VARCHAR(MAX) = '';
SELECT CAST(ROW_NUMBER() OVER (ORDER BY name) AS INT) AS number
INTO #numbers
FROM master..spt_values
;WITH numbers (number)
AS
(
SELECT CAST(ROW_NUMBER() OVER (ORDER BY name) AS INT) AS number
FROM master..spt_values
),a AS
(
SELECT number FROM numbers WHERE number < 10
)
SELECT @text = @text + LTRIM(STR(a.number))
FROM a
ORDER BY a.number DESC
;WITH numbers (number)
AS
(
SELECT number FROM #numbers
),
a
AS
(
SELECT number FROM numbers WHERE number < 10
)
SELECT @text2 = @text2 + LTRIM(STR(a.number))
FROM a
ORDER BY a.number DESC
SELECT @text, @text2;
Hice una muestra y su primera consulta funciona SqlFiddleDemo . Pero su solución depende en gran medida del plan de ejecución.
Use XML + COSAS para la concatenación en su lugar.