wasAsked.

What is a trigger in SQL Server and when should you use one?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · SQL

Community answers

Posted by wasAsked editorial collection

A trigger is a special stored procedure that runs automatically when a specific event happens on a table, view or the database. You do not call it directly; the database fires it. Types in SQL Server 1. DML triggers — fire on INSERT, UPDATE or DELETE. - AFTER (or FOR) triggers run after the statement and constraints succeed. - INSTEAD OF triggers run in place of the statement — commonly used to make complex views updatable. 2. DDL triggers — fire on schema events such as CREATE TABLE, ALTER TABLE, DROP TABLE, at database or server level (useful for auditing schema changes). 3. Logon triggers — fire when a user session is established (for example to restrict logins). The inserted and deleted tables Inside DML triggers, SQL Server provides two virtual tables: - inserted: new row versions (for INSERT and UPDATE). - deleted: old row versions (for DELETE and UPDATE). Example — audit price changes CREATE TRIGGER trg_Products_PriceAudit ON dbo.Products AFTER UPDATE AS BEGIN SET NOCOUNT ON; INSERT INTO dbo.PriceHistory (ProductId, OldPrice, NewPrice, ChangedAt, ChangedBy) SELECT d.ProductId, d.Price, i.Price, SYSUTCDATETIME(), SUSER_SNAME() FROM inserted i JOIN deleted d ON d.ProductId = i.ProductId WHERE i.Price <> d.Price; END; Important: triggers fire once per statement, not once per row. A single UPDATE affecting 1,000 rows fires the trigger once with 1,000 rows in inserted and deleted. Always write set-based logic as above; code that assumes one row (SELECT @id = ProductId FROM inserted) is a classic bug. Good use cases - Auditing and history: recording who changed what and when (though temporal tables and change data capture are often better options). - Enforcing complex integrity rules that constraints cannot express, especially across tables. - Maintaining legacy denormalized data when application changes are not possible. - INSTEAD OF triggers on views to route inserts to underlying tables. - DDL auditing of schema changes. Why to use triggers sparingly - Hidden logic: developers may not know a trigger exists, which makes behaviour surprising and debugging harder. - Performance: triggers run inside the same transaction, adding work and locks to every affected statement; heavy triggers slow down writes. - Cascading and recursive triggers can create complex chains. - Harder to test and to version alongside application code. - Bulk operations: some bulk loads skip triggers unless configured (FIRE_TRIGGERS option). Alternatives to consider - Constraints (CHECK, FOREIGN KEY, UNIQUE) for data rules. - System-versioned temporal tables for automatic history. - Change Data Capture or Change Tracking for downstream synchronisation. - Application-level logic, EF Core SaveChanges interceptors, or the outbox pattern for events. - Stored procedures that make the logic explicit. Interview summary: "A trigger is code that runs automatically on INSERT, UPDATE, DELETE or DDL events. In SQL Server they use the inserted and deleted tables and fire per statement. They are useful for auditing and complex integrity rules, but should be used sparingly because they hide logic and add overhead."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics