AboutSQL Server, Analytics, .Net, Machine Learning, R, Python Archives
About Me
Mitch Wheat has been working as a professional programmer since 1984, graduating with a honours degree in Mathematics from Warwick University, UK in 1986. He moved to Perth in 1995, having worked in software houses in London and Rotterdam. He has worked in the areas of mining, electronics, research, defence, financial, GIS, telecommunications, engineering, and information management. Mitch has worked mainly with Microsoft technologies (since Windows version 3.0) but has also used UNIX. He holds the following Microsoft certifications: MCPD (Web and Windows) using C# and SQL Server MCITP (Admin and Developer). His preferred development environment is C#, .Net Framework and SQL Server. Mitch has worked as an independent consultant for the last 10 years, and is currently involved with helping teams improve their Software Development Life Cycle. His areas of special interest lie in performance tuning |
Friday, May 28, 2010Using SMO to Create an Index with Included ColumnsA question that came up on the SqlDownUnder mailing list today was how to create an index using SMO and specify an included column. The documentation is extremely scarce; I could find no mention of it in MSDN. Greg Low came to the rescue by noting that the IndexedColumn class has an IsIncluded property. This is illustrated in the following C# snippet:
public Index CreateIndex ( string indexName, bool isClustered, IndexKeyType indexKeyType, string[] indexColumnList, // in index column order bool[] indexDescending, string[] includedColumnList ) { if (indexDescending != null && indexDescending.Length != indexColumnList.Length) { throw new ArgumentOutOfRangeException( "Either pass indexDescending as null, or with same length as indexColumnList."); }
Index index = new Index { Name = indexName, IndexKeyType = indexKeyType, IsClustered = isClustered };
int i = 0; foreach (string indexColumnName in indexColumnList) { bool descending = (indexDescending != null) ? indexDescending[i] : false; IndexedColumn indexedColumn = new IndexedColumn(index, indexColumnName, descending); index.IndexedColumns.Add(indexedColumn); i++; }
// Only add included columns for none primary or unique indexes if (includedColumnList != null && indexKeyType == IndexKeyType.None) { foreach (string includedColumnName in includedColumnList) { IndexedColumn indexedColumn = new IndexedColumn(index, includedColumnName); indexedColumn.IsIncluded = true; index.IndexedColumns.Add(indexedColumn); } }
return index; } |
ContactMSN, Email: mitch døt wheat at gmail.com LinksFavorites
Blogs |