Building a notification system directly inside SQL Server is a powerful way to track important data events without depending only on application code.
This article explains how to design a trigger-driven notification system using a queue table, with practical scripts, architecture, and implementation steps.
Overview
Many applications need to notify users when something changes in the database:
A new order is created
A stockline quantity changes
A record is updated or deleted
A threshold is crossed
Instead of writing custom logic in every API, we can centralise this inside SQL using:
A queue table to store notifications
Triggers to detect changes
A polling API or SQL Job to process notifications
A frontend UI to display alerts
Core Workflow (Smaller Diagram Style)
Notification Workflow
[User Action/API]
↓
[Table Change]
↓
[Trigger Executes]
↓
[Queue Table Inserts Notification]
↓
[API Polls or Job Reads Queue]
↓
[Frontend Receives Notification]
1. Designing the Notification Queue Table
CREATE TABLE NotificationQueue (
NotificationId BIGINT IDENTITY(1,1) PRIMARY KEY,
EntityName VARCHAR(100),
EntityId BIGINT,
ActionType VARCHAR(20), -- Insert, Update, Delete
OldData NVARCHAR(MAX) NULL,
NewData NVARCHAR(MAX) NULL,
CreatedDate DATETIME DEFAULT GETDATE(),
IsProcessed BIT DEFAULT 0
);
Why this structure?
Stores who/what changed
Holds before and after data
Supports unprocessed vs processed state
Enables dashboards, API, or background jobs to read unread notifications
2. Creating a Trigger to Capture Changes
Example table: StockLine
Insert Trigger
CREATE TRIGGER trg_StockLine_Insert
ON StockLine
AFTER INSERT
AS
BEGIN
INSERT INTO NotificationQueue (EntityName, EntityId, ActionType, NewData)
SELECT
'StockLine',
i.StockLineId,
'Insert',
(SELECT * FROM INSERTED i FOR JSON AUTO)
FROM INSERTED i;
END
Update Trigger
CREATE TRIGGER trg_StockLine_Update
ON StockLine
AFTER UPDATE
AS
BEGIN
INSERT INTO NotificationQueue (EntityName, EntityId, ActionType, OldData, NewData)
SELECT
'StockLine',
i.StockLineId,
'Update',
(SELECT * FROM DELETED d FOR JSON AUTO),
(SELECT * FROM INSERTED i FOR JSON AUTO)
FROM INSERTED i;
END

Join the conversation! Your thoughts help the community grow.