|
21 | 21 | - [Array](#array) |
22 | 22 | - [Single Row](#single-row) |
23 | 23 | - [Sample with multiple Bindings](#sample-with-multiple-bindings) |
| 24 | + - [Trigger Binding](#trigger-binding) |
| 25 | + - [function.json Properties for Trigger Bindings](#functionjson-properties-for-trigger-bindings) |
| 26 | + - [Setup for Trigger Bindings](#setup-for-trigger-bindings) |
24 | 27 |
|
25 | 28 | ## CSharp Scripting |
26 | 29 |
|
@@ -226,3 +229,67 @@ See the [AddProduct](https://github.com/Azure/azure-functions-sql-extension/tree |
226 | 229 | ### Sample with multiple Bindings |
227 | 230 |
|
228 | 231 | See the [GetAndAddProducts](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-csx/GetAndAddProducts) sample |
| 232 | + |
| 233 | + |
| 234 | +## Trigger Binding |
| 235 | + |
| 236 | +See [Trigger Binding Overview](./BindingsOverview.md#trigger-binding) for general information about the Azure SQL Trigger binding. |
| 237 | + |
| 238 | +### function.json Properties for Trigger Bindings |
| 239 | + |
| 240 | +The following table explains the binding configuration properties that you set in the *function.json* file. |
| 241 | + |
| 242 | +|function.json property | Description| |
| 243 | +|---------|----------------------| |
| 244 | +|**type** | Required. Must be set to `sqlTrigger`.| |
| 245 | +|**direction** | Required. Must be set to `in`. | |
| 246 | +|**name** | Required. The name of the variable that represents the set of changes that triggered the function code. Must be set to `changes`. | |
| 247 | +| **tableName** | Required. The name of the table to be monitored for changes. | |
| 248 | +| **connectionStringSetting** | Required. The name of an app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0). | |
| 249 | + |
| 250 | +### Setup for Trigger Bindings |
| 251 | + |
| 252 | +Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](./GeneralSetup.md#create-a-sql-server). |
| 253 | + |
| 254 | +- Open your app in VS Code |
| 255 | +- Press 'F1' and search for 'Azure Functions: Create Function' |
| 256 | +- Choose HttpTrigger -> (Provide a function name) -> anonymous |
| 257 | +- In the folder that is created with the provided function name, open the file (`run.csx`), replace its contents block with the below code. Note that the casing of the Object field names and the table column names must match. |
| 258 | + |
| 259 | + ```csharp |
| 260 | + #load "Product.csx" |
| 261 | + #r "Newtonsoft.Json" |
| 262 | + #r "Microsoft.Azure.WebJobs.Extensions.Sql" |
| 263 | +
|
| 264 | + using System.Net; |
| 265 | + using Microsoft.AspNetCore.Mvc; |
| 266 | + using Microsoft.Extensions.Primitives; |
| 267 | + using Newtonsoft.Json; |
| 268 | + using Microsoft.Azure.WebJobs.Extensions.Sql; |
| 269 | +
|
| 270 | + public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log) |
| 271 | + { |
| 272 | + log.LogInformation("SQL Changes: " + JsonConvert.SerializeObject(changes)); |
| 273 | + } |
| 274 | + ``` |
| 275 | + |
| 276 | +- We also need to add the SQL trigger binding for the Products table by defining `bindings.changes` property. Open the function.json file. |
| 277 | +- Paste the below in the file as an additional entry to the "bindings": [] array. |
| 278 | + |
| 279 | + ```json |
| 280 | + { |
| 281 | + "name": "changes", |
| 282 | + "type": "sqlTrigger", |
| 283 | + "direction": "in", |
| 284 | + "tableName": "dbo.Products", |
| 285 | + "connectionStringSetting": "SqlConnectionString" |
| 286 | + } |
| 287 | + ``` |
| 288 | + |
| 289 | + *In the above, "dbo.Products" is the name of the table our trigger binding is triggered on. The line below is similar to the input binding and specifies where our SqlConnectionString is. For more information on this, see the [function.json Properties for Trigger Bindings](#functionjson-properties-for-trigger-bindings) section* |
| 290 | + |
| 291 | +- Open the local.settings.json file, and in the brackets for "Values," verify there is a 'SqlConnectionString.' If not, add it. |
| 292 | +- Hit 'F5' to run your code. |
| 293 | +- Update, insert, or delete rows in your SQL table while the function app is running and observe the function logs. |
| 294 | +- You should see the new log messages in the Visual Studio Code terminal containing the values of row-columns after the update operation. |
| 295 | +- Congratulations! You have successfully created your first SQL trigger binding! |
0 commit comments