?
Solved

"Date Modified" column

Posted on 2008-09-30
5
Medium Priority
?
203 Views
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.
thanks!
0
Comment
Question by:GEHC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Accepted Solution

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

Open in new window

0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 1000 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.
0
 
LVL 5

Expert Comment

by:adlink_la
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.  
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
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.
0
 

Author Closing Comment

by:GEHC
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.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What we learned in Webroot's webinar on multi-vector protection.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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