Today's question comes from Julian.
I have a table with several fields including updated
,year
,updatedate
as well as other tables. I am trying to create a trigger that fires upon insert and update to the table. What I want it to do is to put the current date and time (getdate()
) intoupdatedate
andyear(getdate())
intoyear
and setupdated
to 1.I have tried various ways of doing this using an
UPDATE
SQL statement but this just updates every record in the table. I have tried to limit the records changed by using aWHERE TABLE.RECORDID = INSERTED.RECORDID
but this just produces an error aboutinserted.recordid
not existing. I tried anUPDATE
statement on theINSERTED
table but SQL complains that I can't update theINSERTED
table.How would I go about doing this? Thanks!
Julian
The problem is likely that you are not referring to the INSERTED
table in
your UPDATE
Statement. I have found the following code is the simplest,
most straightforward way to approach this:
|
This will work for INSERT
or UPDATE
Tom