Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1031585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:57:09+00:00 2026-05-16T13:57:09+00:00

Is there any simple way to create a column in MS SQL that will

  • 0

Is there any simple way to create a column in MS SQL that will track the last time a record was updated?

I would like to have two fields. One to track when the record was created. That one is simple. Create a datetime field and set its default to getdate(). However the second field seams to be a bit more tricky. I want it to have the latest date (and time) the record was modified.

My options are:

  • Include getdate() in every update statement – not an option, these tables will be accessed from MS Access
  • Allow updates only through an SP. – not an option, these tables will be accessed from MS Access
  • Create triggers for each table – the DB is recreated on many machines and I am afraid it will conflict or be forgotten or get out of synch.

Are there any other options?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T13:57:10+00:00Added an answer on May 16, 2026 at 1:57 pm

    Option 4:

    Create a stored procedure that automatically creates triggers for all the tables in your database. In SQL 2005, optionally run this trigger any time any table is created (using a DDL trigger).

    CREATE PROC UpdateTriggersCreate
    AS
    DECLARE
       @TableSchema sysname,
       @TableName sysname,
       @PrimaryKeys nvarchar(4000),
       @ObjectName nvarchar(4000)
       @TriggerName nvarchar(4000),
       @SQL nvarchar(4000);
    SET @TableName = '';
    SET @TableSchema = '';
    WHILE 1 = 1 BEGIN
       SELECT TOP 1
          @TableSchema = TABLE_SCHEMA,
          @TableName = TABLE_NAME
       FROM INFORMATION_SCHEMA.COLUMNS
       WHERE
          COLUMN_NAME = 'LastUpdatedDate'
          AND (
             TABLE_SCHEMA > @TableSchema
             OR (
                TABLE_SCHEMA = @TableSchema
                AND TABLE_NAME > @TableName
             )
          )
       ORDER BY TABLE_SCHEMA, TABLE_NAME;
       IF @@RowCount = 0 BREAK;
    
       IF NOT EXISTS (
          SELECT *
          FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
          WHERE
             C.CONSTRAINT_TYPE = 'PRIMARY KEY'
             AND C.TABLE_SCHEMA = @TableSchema
             AND C.TABLE_NAME = @TableName
       ) BEGIN
           PRINT '-- Not processing table ''' + @TableSchema + '.' + @TableName + ''' because automatic last updated triggers cannot be used on tables with no primary key.';
           CONTINUE;
       END;
    
       SET @PrimaryKeys = NULL;
       SELECT @PrimaryKeys = Coalesce(@PrimaryKeys + ' AND T.', 'T.') + QuoteName(Y.COLUMN_NAME) + ' = I.' + QuoteName(Y.COLUMN_NAME)
       FROM
          INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
          INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE Y
             ON T.CONSTRAINT_CATALOG = Y.CONSTRAINT_CATALOG
             AND T.CONSTRAINT_SCHEMA = Y.CONSTRAINT_SCHEMA
             AND T.CONSTRAINT_NAME = Y.CONSTRAINT_NAME
       WHERE
          T.CONSTRAINT_TYPE = 'PRIMARY KEY'
          AND T.TABLE_SCHEMA = @TableSchema
          AND T.TABLE_NAME = @TableName;
       -- order is not important which is good because ORDER BY is unreliable in this case
    
       SET @ObjectName = @TableSchema + '.' + @TableName;
       SET @TriggerName = 'TR_' + Replace(@ObjectName, '.', '_') + '_U_TimeUpdated';
       SET @SQL = 'IF Object_ID(''' + @TriggerName + ''', ''TR'') IS NOT NULL DROP TRIGGER ' + @TriggerName;
       EXEC sp_executesql @SQL;
       SET @SQL = 'CREATE TRIGGER ' + @TriggerName + ' ON ' + @ObjectName + ' FOR INSERT
    AS
    SET NOCOUNT ON
    UPDATE T
    SET T.LastUpdatedDate = GetDate()
    FROM
       ' + @ObjectName + ' T
       INNER JOIN Inserted I ON ' + @PrimaryKeys;
    
       EXEC sp_executesql @SQL;
    END;
    

    Once you have a stored procedure like this, schedule it to run once a day or (in sql 2005 and up) in response to the DDL creation of tables.

    Update 1

    The code now handles schema properly, and looks up the primary keys. It also reports on and skips tables that have no primary key.

    I’m not sure if I worked out all the syntax errors–I adapted it from code I’ve done this in before and didn’t actually test it. I’m sure you can figure it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there any simple way to programatically colorize images in .NET? Basically we have
Is there any simple way to generate a default crud (given an entity) with
Is there a freely available library to create a MPEG (or any other simple
I hope this is a simple enough question for any SQL people out there...
Is there any way to have something that looks just like a file on
Is there any simple algorithm to determine the likeliness of 2 names representing the
I'm wondering if there are any simple ways to get a list of all
Very simple question, is there any cloud server enviroments avaliable these days for us
Can someone post any simple explanation of cache aware algorithms? There are lot of
I need a simple app to edit database tables. Are there any code generators

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.