Solved

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

Posted on 2006-11-08
11
775 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
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 85 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 83 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Assisted Solution

by:prajapati84
prajapati84 earned 83 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 26

Assisted Solution

by:Zberteoc
Zberteoc earned 83 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 83 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 83 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

679 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