• you will want to change the order in which columns are added to the script to:

    DECLARE cIxColumn CURSOR FOR

    SELECT SC.Name

    FROM Sys.Index_Columns IC

    JOIN Sys.Columns SC ON IC.Object_ID = SC.Object_ID AND IC.Column_ID = SC.Column_ID

    WHERE IC.Object_ID = @IxTableID AND Index_ID = @IxID

    ORDER BY IC.key_ordinal

    key_ordinal is the order in which the columns are built in the index; Index_Column_ID is the column ID, which does not correlate to the structure of the index.