• Eirikur Eiriksson (5/22/2015)


    Lynn Pettis (5/22/2015)


    Here is a snippet I found in a recently developed piece of code:

    Stored proc parameter: @reportstring NVARCHAR(max)

    DECLARE @tmpTbl TABLE ( value NVARCHAR(200) )

    SELECT @Tmp = ''

    SELECT @i = 1

    SELECT @length = LEN(@reportstring)

    WHILE (@i <= @length)

    BEGIN

    SELECT @char = SUBSTRING(@reportstring, @i, 1)

    IF (@char = ',')

    BEGIN

    INSERT INTO @tmpTbl SELECT @Tmp

    SELECT @Tmp = ''

    END

    ELSE

    BEGIN

    SELECT @Tmp = @Tmp + @char

    END

    SELECT @i = @i + 1

    END

    INSERT INTO @tmpTbl SELECT @Tmp

    --select * from @tmpTbl

    I remember using similar/same method a while back, performed quite nicely on 2K and earlier systems but it's been a while. This kind of loops can sometimes work well but the fact that it's not an iTVFN marks it as a non-contender before the race starts.

    😎

    Actually, this is embedded in a stored procedure. And there are numerous inefficiencies in the code, multiple queries (nested IF's to determine which runs), loads of implicit data conversions, and minor issues that set off my SQL OCD.