I found a little bug in the code. I tested some of the code for clustered indexes together with other indexes and it failed because when looking for the columns of
the clustered indexes it also added those for the non-clustered indexes. The red part I added to solve this problem.
COLUMN_NAME=
(SELECT name FROM sys.columns E WHERE E.OBJECT_ID=B.object_id AND E.column_id=D.column_id),
D.is_descending_key,
C.is_unique
FROM SYS.OBJECTS B
INNER JOIN sys.INDEXES C ON
B.object_id=C.object_id
INNER JOIN sys.index_columns D ON
B.object_id=D.object_id AND D.index_id=1
WHERE B.TYPE='U'
AND (C.index_id=1)
AND B.object_id=@TAB_ID1