Adding Table Trigger for Insert & Last Modified Time in SQL Server 2016

In this post, we will show the simple steps adding triggers for your table To make it more clear, Firstly, we will create a User Table in the SQL database, then add two triggers to this table.

If you need more details about Trigger in SQL Server, please check the Microsoft official documentation. From there, you may add more trigger event and action to your tables. Although trigger is part of a table, it can affect the other tables as well with related actions.

  1. Create a simple table.

Here is the design view of the SQL Server Studio Management.

2. Add the Trigger

If you need the detailed description, please check the last Trigger documentation of the SQL Server.

Here we will add two triggers, User_Insert_Trigger and User_Update_Trigger. Every time a record is added or modified, the columns LastModified and CreatedTime will be updated by SQL Server.

Create User_Update_Trigger

Create User_Insert_Trigger

3. Test Triggers

Insert a record into our table.  Added with created time and last modified time.

Update this record, then the last modified time is changed.

Leave a reply:

Your email address will not be published.

Site Footer