Monitoring Databases  «Prev  Next»

Lesson 10Run sqlmaint.exe
ObjectiveRun 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.

Run 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.


What sqlmaint can do

Auth and syntax essentials

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.

Return codes and history


SQL Server 2022: Modern Maintenance Enhancements

Modern alternatives (recommended)

Exercise

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.


SEMrush Software 10 SEMrush Banner 10