Solved

Trigger question

Posted on 2003-11-17
4
322 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
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now