Solved

Trigger question

Posted on 2003-11-17
4
332 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 125 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

726 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