Solved

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

Posted on 2006-11-08
11
765 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
11 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
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 85 total points
Comment Utility



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:lappins
lappins earned 83 total points
Comment Utility
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
 
LVL 5

Assisted Solution

by:prajapati84
prajapati84 earned 83 total points
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 83 total points
Comment Utility
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 83 total points
Comment Utility
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 83 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now