| Lesson 10 | Run sqlmaint.exe |
| Objective | Run database integrity checks, stats updates, and backups from the command line using sqlmaint, with modern cautions and examples, and explore SQL Server 2022 features for enhanced maintenance. |
sqlmaint.exe (with modern guidance and SQL Server 2022 enhancements)sqlmaint is a legacy utility that can perform DBCC checks, back up databases and logs, update statistics, rebuild indexes, and write text/HTML reports. It remains functional in SQL Server 2022 for backward compatibility but is deprecated and removed in future versions. For new work, prefer Maintenance Plans (SQL Agent + SSIS), direct T-SQL, or SQL Server 2022’s advanced features for performance and maintenance.
sqlmaint can do-CkDB, -CkDBNoIdx, -CkAl, -CkAlNoIdx, -CkCat-UpdOptiStats (optionally with -SupportComputedColumn)-RebldIdx -BkUpDB or -BkUpLog with -BkUpMedia DISK|TAPE, plus -UseDefDir, -CrBkSubDir, -DelBkUps , -VrfyBackup, -BkUpOnlyIfClean-Rpt, -HtmlRpt, -DelHtmlRpt , -WriteHistory-PlanName or -PlanID (uses the plan’s database list only)REM Windows Authentication is used by default if -U is omitted
sqlmaint -S MyServer\SQL2022 ^
-D AdventureWorks2022 ^
-CkDB -CkAl -CkCat ^
-Rpt "C:\DBMaint\AdvWorks_chk.rpt" ^
-WriteHistory
REM Update stats with a 15% sample for all DBs in a plan
sqlmaint -S MyServer -PlanName "UserDBs" -UpdOptiStats 15 -WriteHistoryREM Back up DB to default backup dir, one subdir per DB, keep last 2 weeks
sqlmaint -S MyServer -PlanName "UserDBs" -BkUpDB -BkUpMedia DISK -UseDefDir -CrBkSubDir -DelBkUps 2weeks -VrfyBackupREM Backup only if checks pass
sqlmaint -S MyServer -D Sales -CkDB -BkUpDB "D:\SqlBackups" -BkUpMedia DISK -BkUpOnlyIfClean -VrfyBackup
Tip: Avoid embedding passwords on the command line. Use Windows Authentication (omit -U and -P) or, in SQL Server 2022, leverage Microsoft Entra ID authentication (formerly Azure AD) for secure connections, which integrates with hybrid environments for enhanced security.
0 success, 1 failure (including DBCC errors).-WriteHistory records to msdb.dbo.sysdbmaintplan_history. Reports also write to the specified -Rpt/-HtmlRpt destination with a timestamped filename.sqlmaint -UpdOptiStats), as the Query Optimizer adapts to workload changes dynamically. For example, DOP Feedback adjusts parallelism for maintenance tasks like DBCC CHECKDB, improving performance on large databases.sqlmaint reports. DBAs can use Query Store hints to shape execution plans for maintenance scripts, reducing resource contention during tasks like ALTER INDEX ... REBUILD.sqlmaint -BkUpDB with T-SQL like: BACKUP DATABASE AdventureWorks2022 TO DISK = 'D:\Backups\AdvWorks.bak' WITH COMPRESSION, COMPRESSION_ALGORITHM = 'ZSTD', COMPRESSION_LEVEL = 'LOW'. This reduces backup times and storage needs, especially for large databases.dtexec to run SSIS packages from the command line, offering more flexibility and better integration with modern SQL Server features than sqlmaint.DBCC CHECKDB, BACKUP DATABASE ... WITH COMPRESSION, ALTER INDEX ... REBUILD, UPDATE STATISTICS) scripted via SQL Agent or sqlcmd. Use Ola Hallengren’s Maintenance Solution for a robust, community-trusted T-SQL framework that leverages SQL Server 2022’s capabilities.Test yourself on switches, safe usage, and SQL Server 2022 maintenance features:
Using sqlmaint.exe and SQL Server 2022 Features - Exercise
The next lesson will show you how to use the database Maintenance Plan Wizard, including how to incorporate SQL Server 2022’s enhanced features for streamlined maintenance.