Execution Flow
Statistics are determined server-side before any updates — the Table and Statistics parameters use SQL
LIKE patterns (PowerShell wildcards */? are converted to %/_). System databases are excluded unless named explicitly. Requires sysadmin or db_owner on targets.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Target SQL Server instance. Default: $env:COMPUTERNAME. |
| -SqlCredential | PSCredential | Optional | SQL authentication credentials. |
| -Database | String | Optional | Database name or wildcard (e.g. Sales*). System databases excluded unless named explicitly. Default: *. |
| -Table | String | Optional | Table name or wildcard pattern. Default: *. |
| -Statistics | String | Optional | Statistic name or wildcard pattern. Default: *. |
| -SamplePercent | Int | Optional | Sample percentage (0 = FULLSCAN). Default: 0. |
| -OnlyModified | Bool | Optional | Only update statistics with modification_counter > 0. Default: $true. |
| -Index | Bool | Optional | Include index-backed statistics. Default: $true. |
| -EnableException | Switch | Optional | Throw exceptions immediately instead of logging and continuing. |
Return Value
Returns one PSCustomObject per updated (or failed) statistic: SqlInstance, Database, Table, Statistic, Status (Success / Failed), Message.
Examples
Example 1 — Update all modified statistics in one database
Invoke-sqmUpdateStatistics -Database 'SalesDB' -SamplePercent 10
Example 2 — FULLSCAN on all user databases
Invoke-sqmUpdateStatistics -SqlInstance "SQL01"
Example 3 — Only column statistics (no index stats), all modified
Invoke-sqmUpdateStatistics -Database 'Sales*' -Index $false
Example 4 — Force update regardless of modification counter
Invoke-sqmUpdateStatistics -Database 'Inventory' -OnlyModified $false -SamplePercent 20
Example 5 — WhatIf to preview affected statistics
Invoke-sqmUpdateStatistics -Database 'SalesDB' -WhatIf