?
Solved

Trigger question

Posted on 2003-11-17
4
Medium Priority
?
340 Views
Last Modified: 2008-02-01
I have a field in my database table that records the date a record was last modified.  I would like that value to be updated automatically.  What would the trigger look like to accomplish this.

Thanks
0
Comment
Question by:dbashley1
[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
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 9763573
Hi dbashley1,

Something like:

Create Trigger .... For Update As
Update MyTable Set LastModified = GetDate() Where MyKeyField = Inserted.KeyField

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 

Author Comment

by:dbashley1
ID: 9763781
Tim,

I used the code you wrote but am recieving this error


"The column prefix inserted does not match with a table name or alias name used in the query"

The code for my trigger is:


CREATE TRIGGER setModifyDate ON dbo.tbOrders
FOR  UPDATE
AS
Update tbOrders Set modifyDate = GetDate() Where idOrder = Inserted.KeyField



Thanks
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 9763811
Inserted.idOrder rather than Inserted.KeyField, that was just an example. You need to make sure you are getting the correct value from the Inserted virtual table which has the same column names as your real table.
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9764333
CREATE TRIGGER tu_MyTable ON MyTable
FOR UPDATE AS
BEGIN

Declare @vDate DateTime

Select @vDate = GetDate()

Update MyTable
Set  LastUpdated = @vDate
FROM inserted i
WHERE i.PrimaryKey = MyTable.PrimarkKey

IF @@ERROR <> 0
BEGIN
  RAISERROR('Update Of MyTable Failed', 16, 1)
   Rollback Trans
   Return -1
END

END


You can define the field as this so that the insert in automatically taken care if.
Create Table MyTable (
....
LastModified  DateTime Default GetDate()
)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
Suggested Courses

765 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