I would like understand whether we can trigger an Azure Function when an entry is created in a SQL database?
I know it's possible with a Logic App.
As of 2022-11-17 there is a new feature on AzFn called "trigger Azure SQL pro Functions" matched with a new AzureSQL feature called "change tracking".
"The Azure SQL trigger uses SQL change tracking functionality to monitor a SQL table for changes and trigger a function when a row is created, updated, or deleted."
Quote: Change tracking is enabled on the database and on the table:
SQL
ALTER DATABASE [SampleDatabase]
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);
ALTER TABLE [dbo].[ToDo]
ENABLE CHANGE_TRACKING;
The SQL trigger binds to a IReadOnlyList<SqlChange<T>>, a list of SqlChange objects each with 2 properties:
AzFn:
using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.WebJobs.Extensions.Sql;
namespace AzureSQL.ToDo
{
public static class ToDoTrigger
{
[FunctionName("ToDoTrigger")]
public static void Run(
[SqlTrigger("[dbo].[ToDo]", ConnectionStringSetting = "SqlConnectionString")]
IReadOnlyList<SqlChange<ToDoItem>> changes,
ILogger logger)
{
foreach (SqlChange<ToDoItem> change in changes)
{
ToDoItem toDoItem = change.Item;
logger.LogInformation($"Change operation: {change.Operation}");
logger.LogInformation($"Id: {toDoItem.Id}, Title: {toDoItem.title}, Url: {toDoItem.url}, Completed: {toDoItem.completed}");
}
}
}
}
More samples for the Azure SQL trigger are available in the GitHub repository.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With