| Author |
Topic |
|
billsox
Yak Posting Veteran
74 Posts |
Posted - 2006-01-10 : 14:50:37
|
| Hi -- I've got a table (in SQL 2000) that has an insert trigger and an update trigger. The insert trigger performs an update of the record(s) being inserted. This update causes the update trigger to be executed. Is there a way to prevent this update trigger from being executed? Thanks,Bill |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-01-10 : 15:29:48
|
| can't u remove (drop) the trigger |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-10 : 15:33:33
|
| Here are 2 ways I can think of (without reviewing your logic of why you are doing this)1. combine your insert and update trigger logic to a single trigger "after insert,update"2. set your server configuration option of "nested triggers" from the default of 1 to 0.(I wouldn't do this because it is a server wide configuration change.)If you want to explain your objectives we may be able to advise a different strategy.Be One with the OptimizerTG |
 |
|
|
billsox
Yak Posting Veteran
74 Posts |
Posted - 2006-01-10 : 15:53:40
|
| TG -- I've got a table with a date_added column and a date_modified column. When a user adds a record to the table, I want the insert trigger to set the date_added column to GETDATE(). Similarly, when a user modifies a record in the table, I want the trigger to set the date_modified column to GETDATE(). But the insert trigger performs an update which causes the update trigger to be executed, which is not what I want. Do you have a better solution? Thanks in advance,Bill |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-01-10 : 16:15:58
|
| Instead of the Insert trigger, create a default constraint on the date_added column with a value of getdate().Or just take care of it in your insert stored procedure.CODO ERGO SUM |
 |
|
|
billsox
Yak Posting Veteran
74 Posts |
Posted - 2006-01-10 : 16:55:26
|
quote: Originally posted by Michael Valentine Jones Instead of the Insert trigger, create a default constraint on the date_added column with a value of getdate().Or just take care of it in your insert stored procedure.
I don't believe that stops a user from entering their own date. A default constraint is only enforced when no value is entered in such a column. I want it to override whatever is put in the date_added, whether the user specifies one or not.Bill |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-10 : 20:40:15
|
Ideally, posting to the table only happens via stored procedures and direct table access is not allowed to users. In that case you can handle both date columns with the SPs. However, if that is not possible then I would combine the logic in a single trigger. (as I mentioned above)use pubsset nocount ongocreate table junk (rowid int primary key, i int, dateAdded datetime null, dateUpdated datetime null)gocreate trigger junk_tr_insUpd on junk after insert, updateasbegin --update update j set j.dateUpdated = getdate() from junk j join deleted d on d.rowid = j.rowid --insert update j set j.dateAdded = getdate() from junk j join inserted i on i.rowid = j.rowid left join deleted d on d.rowid = i.rowid where d.rowid is nullendgoinsert junk (rowid, i)select 1, 10 unionselect 2, 20 unionselect 3, 30select * from junkupdate junk set i = 200where rowid = 2select * from junkgodrop table junk Be One with the OptimizerTG |
 |
|
|
billsox
Yak Posting Veteran
74 Posts |
Posted - 2006-01-11 : 09:24:41
|
Sweet. I love the solution. Will I run into a problem if the user deliberately modifies date_updated? Shouldn't that cause never-ending recursive calls? Thanks,Billquote: Originally posted by TG Ideally, posting to the table only happens via stored procedures and direct table access is not allowed to users. In that case you can handle both date columns with the SPs. However, if that is not possible then I would combine the logic in a single trigger. (as I mentioned above)use pubsset nocount ongocreate table junk (rowid int primary key, i int, dateAdded datetime null, dateUpdated datetime null)gocreate trigger junk_tr_insUpd on junk after insert, updateasbegin --update update j set j.dateUpdated = getdate() from junk j join deleted d on d.rowid = j.rowid --insert update j set j.dateAdded = getdate() from junk j join inserted i on i.rowid = j.rowid left join deleted d on d.rowid = i.rowid where d.rowid is nullendgoinsert junk (rowid, i)select 1, 10 unionselect 2, 20 unionselect 3, 30select * from junkupdate junk set i = 200where rowid = 2select * from junkgodrop table junk Be One with the OptimizerTG
|
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-01-11 : 11:36:36
|
>>Will I run into a problem if the user deliberately modifies date_updated? Shouldn't that cause never-ending recursive calls?No, but you can test it yourself with code block above Be One with the OptimizerTG |
 |
|
|
|