# Tuesday, August 03, 2010
« Upgraded das Blog and Hosts | Main | Tip of the Day: T-SQL Script to rename b... »

Today I was setting up a new template, and I needed to look at the unique constraints as part of my task.

The following CodeSmith script will load the columns that have unique constraints (but not the primary key) into an ArrayList.

ArrayList uniqueColumns = new ArrayList();
        for (int i = 0; i < SourceTable.Indexes.Count; i++)
        {
            if (SourceTable.Indexes[i].IsUnique)
            {
                for (int y=0; y < SourceTable.Indexes[i].MemberColumns.Count; y++)
                {
                    if (!SourceTable.Indexes[i].MemberColumns[y].IsPrimaryKeyMember)
                        uniqueColumns.Add(SourceTable.Indexes[i].MemberColumns[y].Name);
                }
            }
            
        }
Comments are closed.