Running Scheduled Tasks in SQL Server Express

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

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
example .bat file for sqlcmd (notepad++)
sqlcmd bat file
example .bat file for sqlcmd (windows explorer)

I’m going to create a new folder for the output log file too:

c:\logs

It’s good to test the script before we create a schedule for it, so I’ll run it now:

c:\logs index maintenance
SQL Server Index Maintenance Log File

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
Task Scheduler Create New Task

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

3. In the Triggers tab, configure the schedule (e.g., weekly at 2 AM).

Task Scheduler New Task Trigger Tab
Task Scheduler Set Schedule

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
Task Scheduler New Task Add .bat File

5. Review settings under the Settings tab, such as maximum runtime limits.

Task Scheduler New Task Set Max Runtime

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

This Scheduled Task has been created in this example and is awaiting its next execution, which is early Sunday morning.

Task Scheduler sqlcmd

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

Hope this was useful for you!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *