"Date Modified" column

Posted on 2008-09-30
Last Modified: 2012-05-05
On SQL Server 2005, looking for the easiest way to add a "date modified" column for all table rows. If adding a trigger is the only way please provide trigger definition/t-sql.
Question by:GEHC
LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 250 total points
ID: 22608552
yes, a trigger is the way to go:
CREATE TRIGGER trg_update_yourtable
 ON yourtable
    SET date_modified = GETDATE()
   FROM yourtable t
     ON t.primary_key_field = i.primary_key_field
    AND t.date_modified < GETDATE()

Open in new window

LVL 39

Assisted Solution

BrandonGalderisi earned 250 total points
ID: 22608597
Adding the Date modified column is the easy part.

alter table YourTable
add last_modified_Dttm datetime null

I use null and no default because I want to differentiate between a record that was and was not modified.

I then leave the update of the last_modified_dttm column to the update stored procedure on the table as ALL of my data access goes through SPs.

Expert Comment

ID: 22608744
If all you want is to have the date_modified column updated, I would go with Brandons approach and set the value in your update procedure.  I would stay away from triggers unless you absolutely need them as they can cause performance problems.  
LVL 39

Expert Comment

ID: 22608771
Triggers are necessary if you don't have absolute control over EVERY (which should be 1) method of updating the data though.  Such as applications with embedded SQL.

Author Closing Comment

ID: 31501697
Yes, I am  developer not a DBA and it IS an app I am having to rewrite. But its mission and task specific not user heavy. I can possible try both approaches. I was hoping though that SQL 2005 had something built -in/update row version.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query 2 43
Steps to produce a data strategy 5 35
SQL Server Question 5 25
Where to download and how to install sqldmo.dll 5 33
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now