Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to automatically update the last modified date in SQL 2000 tables?

Posted on 2006-11-08
11
Medium Priority
?
778 Views
Last Modified: 2008-01-09
Hi,

I have a large database with about 70 tables. I would like to maintain a "Last Modified Date" column on all the tables. What is the best way to automatically update the last modified date whenever a record in a table is updated?

Need this urgently!!!!

Thanks,
0
Comment
Question by:ashugarg00
[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
11 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17897070
ashugarg00,
> What is the best way to automatically update the last modified date
> whenever a record in a table is updated?


Yoou can create a trigger for INSERT AND UPDATE to do this
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 340 total points
ID: 17897108



CREATE TRIGGER trUpdateDate
ON urTable
FOR INSERT,UPDATE
AS
UPDATE A
SET defDate = GETDATE()
FROM Inserted i
INNER JOIN urTable a ON a.PrimaryKey= i.PrimaryKey
GO
0
 
LVL 7

Assisted Solution

by:Stephen Lappin
Stephen Lappin earned 332 total points
ID: 17897384
If you are on an Oracle database, your trigger would look something like this:


CREATE OR REPLACE TRIGGER <trigger_name>
AFTER INSERT OR UPDATE ON <table_name>
FOR EACH ROW
BEGIN
  -- last modified date
 :New.Last_Modified_Date := SYSDATE;
  -- who performed mod
 :New.Last_Modified_User := USER;
END;
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 5

Assisted Solution

by:prajapati84
prajapati84 earned 332 total points
ID: 17897574
Hi ashugarg00,

If the updation or insertion is made to urTables from the code then I think it's very easy to apply getdate() to that column is quite easy. Otherwise you need to create the triggers for all the table that is not good. Actually if you want to update a table when there is a insertion or updation to that table is made then trigger is good but instead you can do it by passing directly the current datetime to that field (Last updated Field). Can u describe ur problem in detail  please?

Regards,
Mukesh
0
 
LVL 27

Assisted Solution

by:Zberteoc
Zberteoc earned 332 total points
ID: 17898884
Just add a column to your table with a name like

last_update of datetime type

and update it along with other fields every time you do an update using

last_update = gatdate() (in MS SQL).

You could also create a "create_date" to store the insert moment of a row. This field should be set as having as default value the same getdate() function, which returns the curent date-time with milisecs.
0
 
LVL 5

Assisted Solution

by:MrNetic
MrNetic earned 332 total points
ID: 17898930
Add an ON UPDATE TRIGGER, and update the column with the getdate().

Best Regards,

Paulo Condeça.
0
 
LVL 2

Assisted Solution

by:stdcitunit
stdcitunit earned 332 total points
ID: 17905181
If you are talking about putting a datestamp on each table, just add a datetime column (date_modified) if you don't already have it, and then create a trigger or two on each table.

alter table [table name]
add [date_modified] datetime not null
go

create trigger [Trigger name]
on [affected_table]
for  update AS
update t1
set [last_moodified] = getdate()
[affected table] t1 inner join updated u on t1.key = u.key
go

create trigger [Trigger name]
on [affected_table]
for  update AS
update t1
set [last_moodified] = getdate()
[affected table] t1 inner join inserted i on t1.key = i.key
go

Is this what you are asking for??? or are you talking about a table that is to contain all the table names with the last modified date??
0
 
LVL 5

Expert Comment

by:MrNetic
ID: 17905187
If it is for logging pursoses,

The best practice is to create an After Update Trigger, and then insert or update a record on other table.

Best Regards,

Paulo Condeça.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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