Solved

Trigger question

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
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…

773 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