• Minor alterations at the end:

    -- Commas removed from the end of ON and OFF so variable following arguments work properly with leading commas

    IF (INDEXPROPERTY(@idxTableID, @idxname, 'IsStatistics') = 1)

    SET @sParamSQL = @sParamSQL + 'ON'

    ELSE

    SET @sParamSQL = @sParamSQL + 'OFF'

    -- Fillfactor 0 is actually not a valid percentage on SQL 2008 R2

    IF ISNULL( @FillFactor, 90 ) <> 0

    SET @sParamSQL = @sParamSQL + ' ,FILLFACTOR = ' + CAST( ISNULL( @FillFactor, 90 ) AS VARCHAR(3) )

    IF (@IsPrimaryKey = 1) -- DROP_EXISTING isn't valid for PK's

    BEGIN

    SET @sParamSQL = @sParamSQL + ' ) '

    END

    ELSE

    BEGIN

    SET @sParamSQL = @sParamSQL + ' ,DROP_EXISTING = ON ) '

    END

    SET @sIndexCols = @sIndexCols + CHAR(13) + @sParamSQL

    -- IF THE INDEX IS NOT A PRIMARY KEY - ADD THIS - ELSE DO NOT

    IF (@IsPrimaryKey = 0)

    BEGIN

    SET @sIndexCols = @sIndexCols + ' ON [' + @location + ']'

    END