From 23063ab8ee6ef8f434fd52fdd0ef14f867fd1f37 Mon Sep 17 00:00:00 2001 From: Ola Mackow Date: Thu, 19 Jul 2018 10:43:07 +0100 Subject: [PATCH] fix format index, allow index list --- functions/Format-SqlIndex.ps1 | 77 +++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/functions/Format-SqlIndex.ps1 b/functions/Format-SqlIndex.ps1 index 7c35c6f..f68c117 100644 --- a/functions/Format-SqlIndex.ps1 +++ b/functions/Format-SqlIndex.ps1 @@ -1,45 +1,54 @@ function Format-SqlIndex { - [CmdletBinding()]Param( - [object]$Index - ,[object]$options + [CmdletBinding()]Param( + [Alias('Index')] + [object]$Indexes + , [object]$options ) $_NL = "`r`n" $_TB = "".PadRight(4) - $s = $Index.table_schema - $t = $Index.table_name - $i = $Index.index_name - - $CLUSTERED_OR_NOT = switch($Index.type){ - 1 {"clustered "} - 5 {"clustered columnstore "} - 6 {"columnstore "} - } + foreach ($Index in $Indexes) { + $s = $Index.table_schema + $t = $Index.table_name + $i = $Index.index_name - $is_columnstore = $Index.type -in (5,6) + $UNIQUE_OR_NOT = "" + $keyCols = "" + $inclCols = "" + $includeBlock = "" - if($Index.is_unique){ - $UNIQUE_OR_NOT = "unique " - } + $CLUSTERED_OR_NOT = switch ($Index.type) { + 1 {"clustered "} + 2 {"nonclustered "} + 5 {"clustered columnstore "} + 6 {"columnstore "} + } + $is_columnstore = $Index.type -in (5, 6) - $keyCols = ($Index.KeyColumns | Sort-Object -Property key_ordinal).KeyColumnName -join "$_NL$_TB$_TB," - $inclCols = ($Index.InclColumns).column_name -join "$_NL$_TB$_TB," - $hasInclCols = (($Index.InclColumns | Measure-Object).Count -gt 0) + if ($Index.is_unique) { + $UNIQUE_OR_NOT = "unique " + } - if($hasInclCols){ - $includeBlock = " $_NL$($_TB)include ( $inclCols ) " - } - - $createIndexCommand = "" - $createIndexCommand += "create $UNIQUE_OR_NOT$($CLUSTERED_OR_NOT)index $i $_NL on $s.$t " - if(-not $is_columnstore){ - $createIndexCommand += "( $keyCols )" - $createIndexCommand += "$includeBlock" - } - if($Index.has_filter){$createIndexCommand += "$($_NL)where $($Index.filter_definition)"} - $createIndexCommand += ";$($_NL)go$_NL" - - [PSCustomObject]@{ - CreateIndexCommand = $createIndexCommand + $keyCols = ($Index.KeyColumns | Sort-Object -Property key_ordinal).KeyColumnName -join "$_NL$_TB$_TB," + $inclCols = ($Index.InclColumns).column_name -join "$_NL$_TB$_TB," + $hasInclCols = (($Index.InclColumns | Measure-Object).Count -gt 0) + + if ($hasInclCols) { + $includeBlock = " $_NL$($_TB)include ( $inclCols ) " + } + + $createIndexCommand = "" + $createIndexCommand += "create $UNIQUE_OR_NOT$($CLUSTERED_OR_NOT)index $i $_NL on $s.$t " + if (-not $is_columnstore) { + $createIndexCommand += "( $keyCols )" + $createIndexCommand += "$includeBlock" + } + if ($Index.has_filter) {$createIndexCommand += "$($_NL)where $($Index.filter_definition)"} + $createIndexCommand += ";$($_NL)go$_NL" + + [PSCustomObject]@{ + CreateIndexCommand = $createIndexCommand + inclCols =$inclCols + } } } \ No newline at end of file