Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Schedule a Sql job in Microsoft Azure SQL database?

I have one SQL Agent maintenance job which checks the index fragmentation within a database and rebuilds indexes if required.

This is running well in my test server (Microsoft Sql Server 2012). But my production server is in Azure. Now I want to schedule that job to Azure.

SQL Agent does not exist in Azure SQL Database so how can I schedule a SQL job in Azure DB?

like image 578
user1006544 Avatar asked Dec 05 '14 05:12

user1006544


People also ask

How do I create an SQL Agent job in Azure database?

Pre-requisite: we need to have at least one SQL database with a service level objective of S0 or above. To create an elastic job agent, Navigate to the Azure portal and type elastic job in the search box. Select the Elastic Job agents in the list as shown in the below image. On Elastic Job agents page, click on Add.

How do I schedule a SQL Express Job?

You can create jobs "manually" by creating batch files and SQL script files, and running them via Windows Task Scheduler. Just put both files into the same folder and exeute the batch file via Windows Task Scheduler.

Does Azure SQL have SQL Agent?

SQL Agent is not available in Azure SQL Database or Azure Synapse Analytics.


2 Answers

Since this question was first asked, there is now another alternative to handle this problem:
Azure Functions

Here are a couple of examples that could easily be modified to call a stored procedure that rebuilds your indexes

  • Create a function in Azure that is triggered by a timer
  • Use Azure Functions to connect to an Azure SQL Database

Also see

  • How to maintain Azure SQL Indexes and Statistics - this page has an example stored procedure for rebuilding your indexes that you can download.
  • Reorganize and Rebuild Indexes

A few things to keep in mind with Azure functions

  • They are built on top of Azure Web Jobs SDK and offer additional functionality
  • There are two different pricing models:
    • App Service plan (attach it to an existing plan)
      • Predictable cost model
      • It puts extra load on the same VM used by your web site
    • Consumption plan
      • You get some free processing every month
      • The default maximum run time is 5 minutes to prevent billing problems, but it can be changed via the host.json file.

Edit September 5, 2021 to add additional information

It should be noted that if you need SQL Agent, you have another option now. I would suggest reading up on Azure SQL Managed Instances. You can see a comparison of Azure SQL to Azure SQL Managed instance here in the Microsoft Documentation. With Azure SQL Managed Instances, your transition to the cloud could be a lot simpler since a lot of the on-premise features you are used to are already there (including SQL Server Agent, DB Mail, etc.).

like image 118
David Yates Avatar answered Sep 29 '22 15:09

David Yates


This feature has been rejected by Microsoft, see here.

To quote the post:

Today in Azure there are several alternatives,

1) SQL Database Elastic Jobs http://azure.microsoft.com/en-us/documentation/articles/sql-database-elastic-jobs-overview/

2) The Azure job scheduler http://www.windowsazure.com/en-us/services/scheduler/

3) The new preview of Azure Automation http://azure.microsoft.com/en-us/services/automation/.

4) SQL Server in a VM

Option 1 requires an additional dedicated cloud service, which increases cost. Option 2 is free (I think) as long as you don't run more than once per hour.

like image 37
Dunc Avatar answered Sep 29 '22 15:09

Dunc