Running scheduled tasks in SQL Server Express requires an alternative approach since this edition lacks SQL Server Agent. Tasks like index maintenance, backups, or other recurring operations can be scheduled using Windows Task Scheduler. Below is a straightforward method to set up and execute such tasks using sqlcmd
and a .bat
script.
The following is included in this article:
> Setting Up a Scheduled Task for SQL Server Express
> Create Scheduled Task for sqlcmd Script
Setting Up a Scheduled Task for SQL Server Express
Start by ensuring sqlcmd
is installed on the system. It comes with SQL Server Management Studio (SSMS), or you can download it separately. Confirm its location (commonly under C:\Program Files\Microsoft SQL Server\Client SDK\ODBC
) for use in the script.
My sqlcmd.exe and bcp.exe clients are in:
-> C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Bin
![sqlcmd Location](https://peter-whyte.com/wp-content/uploads/2019/09/sqlcmd_exe_location.png)
If you already have SSMS installed, you can find the directory of sqlcmd
with the help of this Microsoft Documentation Page: MS Docs – File Locations for Default and Named Instances of SQL Server
The sqlcmd
utility is installed with SSMS, or you can download it separately for Windows/Linux environments: MS Docs – Download and install sqlcmd
I’m going to test and run the sqlcmd
command below which will run index maintenance on my SQL instance:
sqlcmd -Q "EXEC master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_updatestats'; EXEC dbo.IndexOptimize @Databases = 'ALL_DATABASES', @FragmentationLow = NULL, @FragmentationMedium = 'INDEX_REBUILD_OFFLINE', @FragmentationHigh = 'INDEX_REBUILD_OFFLINE', @FragmentationLevel1 = 30, @FragmentationLevel2 = 50, @pagecountlevel = 1000" -o "c:\logs\index_maintenance.txt"
Next, I’ll copy this into a notepade and save it as a .bat file:
![sqlcmd .bat index maintenance](https://peter-whyte.com/wp-content/uploads/2019/09/sqlcmd_indexmaint_bat_code.png)
![sqlcmd bat file](https://peter-whyte.com/wp-content/uploads/2019/09/sqlcmd_index_maintenance_bat_file.png)
I’m going to create a new folder for the output log file too:
![c:\logs](https://peter-whyte.com/wp-content/uploads/2019/09/c_logs_empty.png)
It’s good to test the script before we create a schedule for it, so I’ll run it now:
![c:\logs index maintenance](https://peter-whyte.com/wp-content/uploads/2019/09/c_logs_with_log.png)
![SQL Server Index Maintenance Log File](https://peter-whyte.com/wp-content/uploads/2019/09/index_maintenance_log.png)
Now we have our script ready and tested, we can proceed to the next step and schedule the execution of the script.
Create Scheduled Task for sqlcmd Script
Use Windows Task Scheduler to automate the script.
1. Open Task Scheduler and create a new task:
![Open Task Scheduler](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_app.png)
![Task Scheduler Create New Task](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_rightclick.png)
2. In the General tab, provide a name and set the user account for the task. Ensure the account has “Log on as a batch job” permissions.
![Task Scheduler New Task General Tab](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_create_new_task.png)
3. In the Triggers tab, configure the schedule (e.g., weekly at 2 AM).
![Task Scheduler New Task Trigger Tab](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_create_new_task_trig.png)
![Task Scheduler Set Schedule](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_set_schedule.png)
4. In the Actions tab, specify the action as Start a Program, and point to the .bat
file location.
![Task Scheduler New Task Action Tab](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_create_action.png)
![Task Scheduler New Task Add .bat File](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_action_bat_file.png)
5. Review settings under the Settings tab, such as maximum runtime limits.
![Task Scheduler New Task Set Max Runtime](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_settings.png)
Once the task is created, you can force it to run manually for verification or wait for its scheduled execution. Logs and Task Scheduler history can help confirm the script’s success.
You will be prompted to enter a username and password if the task is set to use an AD service account.
![Task Scheduler New Task Password Prompt](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_pass_prompt.png)
This Scheduled Task has been created in this example and is awaiting its next execution, which is early Sunday morning.
![Task Scheduler sqlcmd](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_created-1024x194.png)
In this example I can also verify the job has ran by looking at the output directory.
We can also verify the SQLCMD script ran successfully by checking the Scheduled Task History and log output.
![Index Maintenance Scheduled Task Verification](https://peter-whyte.com/wp-content/uploads/2019/09/task_scheduler_history_and_log-1024x461.png)
Hope this was useful for you!
Leave a Reply