Insert changed and created date

  • Hello!

    I remember seeing somwhere a easy way to add columns to a table that contain the created date of the record and the latest change date of the record.

    These columns are to be updated automatically with the current time when a update or an insert occur.

    Does anyone recognize this and knows how to achive this functionality (without manually writing triggers for each column in each table)?.

  • As for the CreateDate of a row, simply put a DEFAULT of GETDATE() on the column.

    As for the LastChangeDate I guess you will have to write triggers

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Unfortunately, there's no such thing as a free lunch.

    A default constraint can deal with inserts automagically, updates however must be dealt with

    by a trigger, or by the updating statement.

    A good place to start is reading up on constraints in Books On Line (BOL)

    Default constraints also have their rules, and there are some things to think about to make them work as expected.

    But if you need updates to be managed as well, then your options are triggers, or have the updating statement also provide the vaules for the time

    =;o)

    /Kenneth

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply