Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
CVDpr
Starting Member
41 Posts |
Posted - 2008-12-18 : 14:49:26
|
This trigger insert the row in another table when field2 is updated/changed.when using this in sql server 2005 works, but someone tells me when using the sql server 2000 the trigger don't run. Is this code is supposed to work in both 2005/2000 or there is a problem with the sql server 2000 that I'm not aware of?. thanks.alter TRIGGER xxxon tableNameafter updateasbegin set nocount on if update(field2) begin insert into tableName(field1,field2,field3) select field1,field2,field3 from inserted if @@ERROR = 0 begin commit transaction end else begin rollback transaction end endend |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-19 : 06:48:15
|
It looks ok.You may test it.Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-19 : 06:52:23
|
Drop the complete IF @@error thingy.The trigger execution is made in same context as original statement.ALTER TRIGGER xxxON tableNameFOR UPDATEASSET NOCOUNT ON IF UPDATE(Field2) INSERT tableName ( field1, field2, field3 ) SELECT field1, field2, field3 FROM inserted E 12°55'05.63"N 56°04'39.26" |
|
|
CVDpr
Starting Member
41 Posts |
Posted - 2008-12-19 : 08:56:52
|
the trigger still don't work in 2000, someone say to me that i can compare the values between the inserted and the field2. Any know about this? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-19 : 09:03:44
|
Who says that?Compare what? E 12°55'05.63"N 56°04'39.26" |
|
|
CVDpr
Starting Member
41 Posts |
Posted - 2008-12-19 : 09:18:07
|
quote: Originally posted by Peso Who says that?Compare what? E 12°55'05.63"N 56°04'39.26"
That i have to use the inserted table and join with the updated table and compare then insert the values into the other table. :-( |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-19 : 09:35:01
|
Compare what I ask?You mean you want to log the records ONLY if there have been a change in field2?-- Create log tableCREATE TABLE tblLog ( i INT, j INT )-- Create work tableCREATE TABLE tblPeso ( i INT, j INT )-- Create logging triggerGOCREATE TRIGGER trgPesoON tblPesoFOR UPDATEASIF UPDATE(j) INSERT tblLog SELECT i, j FROM insertedGO-- Insert initial values in working tableINSERT tblPesoSELECT 1, 2 UNION ALLSELECT 2, 3-- Check log tableSELECT *FROM tblLog-- Update one record in working tableUPDATE tblPesoSET j = 1WHERE i = 1-- Check log tableSELECT *FROM tblLog-- Clean upDROP TABLE tblLog, tblPeso E 12°55'05.63"N 56°04'39.26" |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-03-12 : 18:13:05
|
THANKS PESO....YOUR TRIGGER HELPED ME A LOT!quote: Originally posted by Peso Compare what I ask?You mean you want to log the records ONLY if there have been a change in field2?-- Create log tableCREATE TABLE tblLog ( i INT, j INT )-- Create work tableCREATE TABLE tblPeso ( i INT, j INT )-- Create logging triggerGOCREATE TRIGGER trgPesoON tblPesoFOR UPDATEASIF UPDATE(j) INSERT tblLog SELECT i, j FROM insertedGO-- Insert initial values in working tableINSERT tblPesoSELECT 1, 2 UNION ALLSELECT 2, 3-- Check log tableSELECT *FROM tblLog-- Update one record in working tableUPDATE tblPesoSET j = 1WHERE i = 1-- Check log tableSELECT *FROM tblLog-- Clean upDROP TABLE tblLog, tblPeso E 12°55'05.63"N 56°04'39.26"
Daipayan |
|
|
|
|
|
|
|