Skip to content

Commit c5a53ef

Browse files
authored
Merge pull request #189824 from Fleid/patch-9
Update sql-database-upsert.md
2 parents 27db8f6 + 6def6d2 commit c5a53ef

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

articles/stream-analytics/sql-database-upsert.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ Update the `sqltext` command building section to match your own schema (notice h
355355

356356
```C#
357357
var sqltext =
358-
$"MERGE INTO [device03] AS old " +
358+
$"MERGE INTO [device_updated] AS old " +
359359
$"USING (VALUES ({DeviceId},{Value},'{Timestamp}')) AS new (DeviceId, Value, Timestamp) " +
360360
$"ON new.DeviceId = old.DeviceId " +
361361
$"WHEN MATCHED THEN UPDATE SET old.Value += new.Value, old.Timestamp = new.Timestamp " +
@@ -380,7 +380,26 @@ Outside of Azure Functions, there are multiple ways to achieve the expected resu
380380

381381
A background task will operate once the data is inserted in the database via the standard ASA SQL outputs.
382382

383-
For Azure SQL, `INSTEAD OF` [DML triggers](/sql/relational-databases/triggers/dml-triggers?view=azuresqldb-current&preserve-view=true) can be used to intercept the INSERT commands issued by ASA and replace them with UPDATEs.
383+
For Azure SQL, `INSTEAD OF` [DML triggers](/sql/relational-databases/triggers/dml-triggers?view=azuresqldb-current&preserve-view=true) can be used to intercept the INSERT commands issued by ASA and replace them with UPDATE or MERGE:
384+
385+
```SQL
386+
CREATE TRIGGER tr_devices_updated_upsert ON device_updated INSTEAD OF INSERT
387+
AS
388+
BEGIN
389+
MERGE device_updated AS old
390+
USING inserted AS new
391+
ON new.DeviceId = old.DeviceId
392+
393+
WHEN MATCHED THEN
394+
UPDATE SET
395+
old.Value += new.Value,
396+
old.Timestamp = new.Timestamp
397+
398+
WHEN NOT MATCHED THEN
399+
INSERT (DeviceId, Value, Timestamp)
400+
VALUES (new.DeviceId, new.Value, new.Timestamp);
401+
END;
402+
```
384403

385404
For Synapse SQL, ASA can insert into a [staging table](../synapse-analytics/sql/data-loading-best-practices.md#load-to-a-staging-table). A recurring task can then transform the data as needed into an intermediary table. Finally the [data is moved](../synapse-analytics/sql-data-warehouse/sql-data-warehouse-tables-partition.md#partition-switching) to the production table.
386405

@@ -418,4 +437,4 @@ For further assistance, try our [Microsoft Q&A question page for Azure Stream An
418437
* [Use managed identities to access Azure SQL Database or Azure Synapse Analytics from an Azure Stream Analytics job](sql-database-output-managed-identity.md)
419438
* [Use reference data from a SQL Database for an Azure Stream Analytics job](sql-reference-data.md)
420439
* [Run Azure Functions in Azure Stream Analytics jobs - Tutorial for Redis output](stream-analytics-with-azure-functions.md)
421-
* [Quickstart: Create a Stream Analytics job by using the Azure portal](stream-analytics-quick-create-portal.md)
440+
* [Quickstart: Create a Stream Analytics job by using the Azure portal](stream-analytics-quick-create-portal.md)

0 commit comments

Comments
 (0)