You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/stream-analytics/sql-database-upsert.md
+22-3Lines changed: 22 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -355,7 +355,7 @@ Update the `sqltext` command building section to match your own schema (notice h
355
355
356
356
```C#
357
357
varsqltext=
358
-
$"MERGE INTO [device03] AS old "+
358
+
$"MERGE INTO [device_updated] AS old "+
359
359
$"USING (VALUES ({DeviceId},{Value},'{Timestamp}')) AS new (DeviceId, Value, Timestamp) "+
360
360
$"ON new.DeviceId = old.DeviceId "+
361
361
$"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
380
380
381
381
A background task will operate once the data is inserted in the database via the standard ASA SQL outputs.
382
382
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
+
CREATETRIGGERtr_devices_updated_upsertON device_updated INSTEAD OF INSERT
387
+
AS
388
+
BEGIN
389
+
MERGE device_updated AS old
390
+
USING inserted AS new
391
+
ONnew.DeviceId=old.DeviceId
392
+
393
+
WHEN MATCHED THEN
394
+
UPDATESET
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
+
```
384
403
385
404
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.
386
405
@@ -418,4 +437,4 @@ For further assistance, try our [Microsoft Q&A question page for Azure Stream An
418
437
*[Use managed identities to access Azure SQL Database or Azure Synapse Analytics from an Azure Stream Analytics job](sql-database-output-managed-identity.md)
419
438
*[Use reference data from a SQL Database for an Azure Stream Analytics job](sql-reference-data.md)
420
439
*[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