Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trigger question

Posted on 2003-11-17
4
Medium Priority
?
344 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
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

618 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