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 |
rockstar283
Yak Posting Veteran
96 Posts |
Posted - 2010-12-17 : 20:29:26
|
hi..I want to create a trigger which will update a particular column of the table e.g i have this table named "investors"..when either of INSERT,UPDATE,DELETE command takes place on any of the records of the entire table, then 'duration' column should be calculated and updated for each recordduration = datediff(month,lockin_date,getdate())Can somebody tell me how to write a trigger for it..I have written the following trigger but it is not workingCREATE TRIGGER [dbo].[investors_update]ON [dbo].[investors]FOR INSERT,UPDATE,DELETE ASDECLARE @diff intBEGINselect @diff = select DATEDIFF(month, lockin_date , getdate()) from investorsupdate investors set duration=@diffENDgo |
|
bobmcclellan
Starting Member
46 Posts |
Posted - 2010-12-17 : 23:19:29
|
this is untested but should get you there Rockstar... Note: hopefully you are testing this in a development environment.Your update query had no params. if it worked you would have updated your whole table.CREATE TRIGGER [dbo].[investors_update]ON [dbo].[investors]FOR INSERT,UPDATE,DELETEASBEGINDECLARE @diff int, @id intset @id = (Select id from inserted)select @diff = (select DATEDIFF(month, i.lockin_date , getdate()) from investors inv inner join Inserted i on inv.id = i.id ) update investors set duration=@diffwhere id = @idEND |
 |
|
Sachin.Nand
2937 Posts |
|
|
|
|
|
|