Announcing public preview of the Azure SQL trigger for Azure Functions
Published Nov 10 2022 03:20 PM 19.4K Views

Invoking an Azure Function from changes to an Azure SQL table is now possible through the Azure SQL trigger for Azure Functions, now available in public preview for C# Azure Functions.  Support for Java and PowerShell has been added to the existing public preview for Azure SQL bindings for Azure Functions, which now supports C#, JavaScript, Python, Java, and PowerShell.

 

Azure SQL trigger

 

The Azure SQL trigger for Azure Functions uses SQL change tracking functionality to monitor a SQL table for changes and trigger a function when a row is created, updated, or deleted.  Change tracking is available for Azure SQL Database, Azure SQL Managed Instance, and SQL Server, making the Azure SQL trigger for Azure Functions a flexible component for event-driven applications.

 

Similarly to the Azure SQL bindings for Azure Functions, a connection string for the SQL database is stored in the application settings of the Azure Function and supporting authentication options such as managed identity. In addition to the connection string, the SQL trigger is configured with a table name. The SQL trigger is specified on lines 12 and 13 in the C# Azure Function example below, which will log information about each change made to data in the dbo.Employees table.

 

 

 

 

using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.Sql;

namespace Company.Function
{
    public static class EmployeeTrigger
    {
        [FunctionName("EmployeeTrigger")]
        public static void Run(
            [SqlTrigger("[dbo].[Employees]", ConnectionStringSetting = "SqlConnectionString")]
            IReadOnlyList<SqlChange<Employee>> changes,
            ILogger logger)
        {
            foreach (SqlChange<Employee> change in changes)
            {
                Employee employee = change.Item;
                logger.LogInformation($"Change operation: {change.Operation}");
                logger.LogInformation($"EmployeeID: {employee.EmployeeId}, FirstName: {employee.FirstName}, LastName: {employee.LastName}, Company: {employee.Company}, Team: {employee.Team}");
            }
        }
    }
}

 

 

 

 

You can learn more about the Azure SQL trigger for Azure Functions, now available in public preview for C# functions at https://aka.ms/sqltrigger.

 

Azure SQL input and output bindings

 

Azure SQL bindings for Azure Functions is now expanded to include support for Java and PowerShell functions. With Azure SQL bindings, data can be input from a database to the function with an input binding. Conversely, data can be output from the function to the database with an output binding. Azure SQL bindings for Azure Functions now support input and output bindings in C#, JavaScript, Python, PowerShell, and Java in public preview.

Configuring these bindings is accomplished with a small number of parameters, including specification of the database query or table and the connection string.  The Java example below will return the rows selected from the query executed on the dbo.Products table to an HTTP request, filtering on the Id column from the value specified in request route.

 

 

 

 

package com.function;

import com.function.Common.Product;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.SQLInput;

import java.util.Optional;

public class GetProduct {
    @FunctionName("GetProduct")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET},
                authLevel = AuthorizationLevel.ANONYMOUS,
                route = "getproduct/{id}")
                HttpRequestMessage<Optional<String>> request,
            @SQLInput(
                commandText = "SELECT * FROM Products WHERE Id = @Id",
                commandType = "Text",
                parameters = "@Id={id}",
                connectionStringSetting = "SqlConnectionString")
                Product[] products) {

        return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
    }
}

 

 

 

 

You can learn more about developing Azure Functions with the Azure SQL bindings for Azure Functions at https://aka.ms/sqlbindings.

 

Get started with Azure Functions and Azure SQL

The library that provides both Azure SQL bindings and the Azure SQL trigger is open source and available on GitHub at https://github.com/Azure/azure-functions-sql-extension.  Samples are available for every supported language in the repository as well as in the documentation.

 

Watch Azure SQL and Azure Functions: Integrating with SQL bindings to see how Azure SQL bindings can help making building an application with Azure SQL easier and faster.

2 Comments
Version history
Last update:
‎Nov 10 2022 02:43 PM
Updated by: