sqlsql-serverdatabasetriggerssql-server-express

How can I do a BEFORE UPDATED trigger with sql server?


I'm using Sqlserver express and I can't do before updated trigger. There's a other way to do that?


Solution

  • MSSQL does not support BEFORE triggers. The closest you have is INSTEAD OF triggers but their behavior is different to that of BEFORE triggers in MySQL.

    SQL Server offers DML triggers

    DML triggers is a special type of stored procedure that automatically takes effect when a data manipulation language (DML) event takes place that affects the table or view defined in the trigger (Insert, Update, Delete).

    DML trigger statements use two special tables: the deleted and inserted tables.

    Note

    INSTEAD OF triggers override the standard actions of the triggering statement. Therefore, they can be used to perform error or value checking on one or more columns and perform additional actions before inserting, updating or deleting the row or rows.

    From the now offline documentation

    Specifies that the trigger is executed instead of the triggering SQL statement, thus overriding the actions of the triggering statements.

    You can learn more about types of dml triggers here

    Thus, actions on the update may not take place if the trigger is not properly written/handled. Cascading actions are also affected.

    You may instead want to use a different approach to what you are trying to achieve.