Schedule Azure SQL DB Processes using Azure Functions

By:   |   Updated: 2021-08-13   |   Comments (2)   |   Related: > Azure Functions


Problem

Azure SQL database is a fully managed Platform as a Service (PaaS) solution, that offers the ability to create, build and migrate native cloud applications at an affordable price. But what if there is a need to schedule a batch process, like using a SQL Server Agent Job for an on-premises instance? There are multiple options to achieve scheduling in the cloud, with each having pros and cons. In this tutorial we will look at the easiest way to achieve scheduling using a Basic Tier database?

Solution

If you are using a basic tier Azure SQL DB, one of the easiest ways to schedule a process is using an Azure Function with a time trigger. An Azure Function is a serverless option to run code in multiple languages, to create cloud native functions without worrying about the underlying infrastructure. Azure functions can be executing by choosing multiple triggers, but for our needs we will use a time trigger, so the function can be executed at set times.

How does this work?

The basic idea is quite simple, we can create a Stored Procedure and then using code of our preference to call the stored procedure from an Azure Function. The function can be scheduled to run at certain times and any code we have in the stored procedure will be executed.

Time trigger function diagram

What is needed?

We will use the latest available Azure function runtime (at the time of writing this tutorial) which is Azure function runtime 3.x. Also, it is requires having an Azure SQL Database, you can check this tip on how to create one. For simplicity we will using the Azure portal to create the function and will be using C# (.Net), but you can use any language you want for your function.

Azure Function Example

I already have an Azure SQL Database in my Azure account which I use for testing as shown below.

Azure SQL DB

I will create a simple stored procedure that will "simulate" a batch process, it will just return a random number that we will be use as our "processed rows."

CREATE PROCEDURE SimulateBatchProcess
AS
BEGIN    
    SET NOCOUNT ON
    SELECT cast(RAND()*1024 as int);
END
GO

That is everything we need from Azure SQL Database, so now let's setup the Azure Function.

Set up Azure Function App

The first step is to go to the Azure Portal and search for Function, which will return the following. Then select Function App.

Create a function App

In the new form, select Create to create a new function.

Create a function App 2

In the new window, enter the following:

  1. The resource group to use.
  2. Put a meaningful App name.
  3. For Publish, we will use the Code option, and then for Runtime stack, select the language you want to use, for our example, we will use .NET (C#) since this can be programmed in the portal.

Then, click Next : Hosting >.

Create a function app, select details

In the next window, enter the following information:

  1. The Storage account to use, if you do not have any, you can select a new one.
  2. For Operating system, select the one you want, I will choose Windows.
  3. For Plan type, and since we want the cheapest one, we will select the Serverless option. You can check the pricing options here.

Then click on Next : Monitoring >.

Create function app, select hosting

I do not need application insights, so I select No. At this point, you can click on the Review + Create button.

Create function app, monitoring

Review that everything is OK and click Create.

Create function app, review

The service will be created in a couple of minutes:

create function app, deployment

Once done, click on Go to resource:

deployment complete

This is the main page for your App service, you can see that you can stop or restart the service if you need to. You can see that you have the function's URL as well, but for this type of function we will not need it, it is just for reference.

Function App overview

The next step is to create the actual function that will run the stored procedure.

Create the Time Trigger Function

In the main page go to Functions.

Create function, select option

In the new page, select the Add option.

Create function, add

In the new window, select the following:

  1. For our example, we will use the Develop in portal option, so we can add the code directly using Azure portal. This option is only available for .NET runtime.
  2. Select the Time trigger option. This is the most important option as this will allow the function to run on a scheduled basis.

After you select the option in step 2, scroll down on the window.

create function, select details

In the Template details, put the following information:

  1. Put a descriptive name for the function.
  2. Schedule is set using CRON format, the default when you create a function is 5 minutes, but for our example I have set it to 1 minute. You can read more about CRON here.

Then click Add.

create function, time trigger

After the function is created, we must enter the function code, so go to Code + Test option.

create function, add code

Note: you can only modify code if the Function App is running.

You will see a template code in the run.csx file, remove the contents.

Create function, run.csx

Using the System.Data.SQLClient assembly, we will use the following code. Just remember to change your connection string and your stored procedure name. This code will run the stored procedure we created earlier and print in the console the execution time and number of rows processed (which is a result set from the stored procedure).

using System;
using System.Data.SqlClient;
 
public static void Run(TimerInfo myTimer, ILogger log)
{
    string SqlConnectionString ="Server=tcp:<Myserver>,1433;Initial Catalog=<myDB>;Persist Security Info=False;User ID=<Username>;Password=<pwd>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
 
    SqlConnection conn = new SqlConnection(SqlConnectionString);
    conn.Open();
 
    SqlCommand command = new SqlCommand("SimulateBatchProcess", conn);
    command.CommandType = System.Data.CommandType.StoredProcedure;
    using (SqlDataReader reader = command.ExecuteReader())
    {
    if (reader.Read())
    {
        log.LogInformation($"Successfully processed at: {DateTime.Now}");
        log.LogInformation($"Rows processed: "+ reader.GetInt32(0).ToString());
    }
    }
 
    conn.Close();
        
}

Replace the run.csx contents with the code above (after making any adjustments) and click Save:

Create function, save code

Testing it!

If everything is OK, the function will execute at the scheduled time, but if you want to manually execute it, select the Test/Run option, with all the defaults and then click Run.

Test function

There might be some warnings (in yellow), but you can ignore those, if the procedure executes OK. You will see the output in blue as shown below:

execution log for function

For the scheduled executions, you will see the messages in the console as shown below:

Function automated execution log

With this we have successfully scheduled a stored procedure using an Azure Function.

Changing the Schedule

If you want to modify the schedule of the job, go to the Code + Test section.

how to change function schedule

Then, in the Drop-down menu, select the function.json file:

select function.json file

In there, you can change the schedule following the same CRON format we discussed before:

change function schedule

Once you are done, click Save and the changes will be applied automatically.

Next Steps
  • You can read up on Azure Functions best practices here.
  • You can read more about Time Trigger Function best practices here.
  • Access more Azure tips here.


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Eduardo Pivaral Eduardo Pivaral is an MCSA, SQL Server Database Administrator and Developer with over 15 years experience working in large environments.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2021-08-13

Comments For This Article




Wednesday, September 14, 2022 - 4:34:59 AM - Jo Back To Top (90478)
2022-09-14T08:27:49Z [Error] Function compilation error
2022-09-14T08:27:49Z [Error] run.csx(9,5): error CS1069: The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.

How do you add a reference to the System.Data.SqlClient assembly if you didn't create the function app in Visual Studio?

Monday, May 16, 2022 - 1:09:45 AM - manoj joon Back To Top (90087)
when I am executing above steps I am getting below error did you have any solution for the same.

2022-05-16T05:08:34.482 [Error] Function compilation errorMicrosoft.CodeAnalysis.Scripting.CompilationErrorException : Script compilation failed.at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.CreateFunctionTarget(CancellationToken cancellationToken) at /_/src/WebJobs.Script/Description/DotNet/DotNetFunctionInvoker.cs : 321at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at async














get free sql tips
agree to terms