What is the difference between INSTEAD OF and AFTER trigger in SQL Server?

What is the difference between INSTEAD OF and AFTER trigger in SQL Server?

In INSTEAD, from a trigger called before a unique key constraint, does the AFTER trigger fire after a unique key constraint?

+4
source share
2 answers

AFTER a trigger fires after a DML operation. INSTEAD of trigger launches a DML operation instead .

A big difference. INSTEAD OF allows you to redefine functionality or implement functionality that is otherwise not supported. The common place I use is to create updatable views. Sometimes a view cannot be stored in a key, but as a designer you can find out which base tables you want to update, so that you can do this by writing specific logic to perform the update backstage. An alternative is to simply write a stored procedure and force developers to call these procedures instead of executing DML in the view, but DML on the views is a good abstraction, in my opinion, because developers can process views such as tables. Here's what a relational design should look like.

As for β€œafter a unique key restriction,” the AFTER trigger will occur after the DML completes successfully, so it will happen after any violations (which will result in a rollback).

+8
source

After starting the trigger after changing the data, instead of starting the trigger before changing the data.

0
source

Source: https://habr.com/ru/post/1304832/


All Articles