Question

this is my trigger in sql server. I want this trigger to fire automatically( i.e daily) instead of update...

create trigger  trig_name
on tab_name
for update
as 
begin
declare   @id int,@innn int
declare @dif int
declare   @inn int
set @id=(select whateverid from inserted)
set @inn = (select DATEDIFF(DAY,INSERTDT,UPDATEDDT) from tab_name where whateverid=@id)
set @innn =(select DATEDIFF(DAY,INSERTDT,GETDATE()) from tab_name where whateverid=@id)
set @dif = @inn-@innn
 update tab_name set due=@dif from tab_name where whateverid= @id
end

No correct solution

OTHER TIPS

Create a new SQL Agent Job, and add a Transact SQL step:

update tab_name 
set due = DATEDIFF(DAY, INSERTDT, UPDATEDDT) - DATEDIFF(DAY, INSERTDT, GETDATE())

Obviously, unlike the trigger, you can't update those that have just been updated. So this will update all 'due' fields based on the time it runs.

I would consider create a stored proc and getting the job to run that instead. It easier manager, and less likely to get missed in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top