• Luis Cazares (4/22/2015)


    Not really, the first top doesn't need and ORDER BY because it's just selecting rows and adding the numbers later.

    No, i mean not the first but the second one

    cteStart(N1) AS (--==== TOP() ajusted to get no more then @cnt rows

    SELECT 1 UNION ALL

    SELECT TOP (@cnt-1) t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter

    ORDER BY t.N

    )

    Without ORDER BY optimizer some day may decide to return arbitrary set of(@cnt-1) rows from cteTally . While it must be exactly first rows.