I have a scheduled job (.Net, WinOS Task Scheduler) which completes shortly after it's called but the SQL work it should accomplish in real time does not happen until several hours later.
The .Net scheduled job calls a stored procedure which updates 2 "top level" tables and outputs 3 data readers (that's what they are to the .Net program). Both updated tables have 2 update triggers each (one trigger inserts to a log table & one manages some computed fields in "quick search" tables). One of the fields updated in each table is a datetime field - set to getdate() within the sproc. Temp tables are used to identify the rows to be updated. Quick pseudo-code (showing just one update and select statement):
declare @mydate dateime
set @mydate = getdate()
declare @UpdateIDs table(MyID int)
set Flag1 = [new value]
, Flag1Date = @mydate
where CaseID in (select MyID from @UpdateIDs)
-- 2 triggers are fired in conjunction with this statement
select [several fields]
from @UpdateIDs t
inner join [several joins from temp table]
The problem: the .Net program runs at 11p each night and completes in about 20 seconds. It sends out an email for each row in the select and one to me regarding the ending status. All those emails happen at 11p. The job reports success.
But the time stamp (the param @mydate & the field TopLevelTable1.Flag1Date) is 6am the next day. The trigger inserting to TopLevelTable1_Log has a field LogDate which is set to getdate() within the trigger and its value is also 6am.
There is no other maintenance work being done at that time. Transaction logs (using sqllogship.exe) are being done and full database backup is done at 1:45am. That full backup has the unmodified rows relating to the this job.
What could be happening?
Have you confirmed that your time zone settings are consistent across servers? SQL Server gets the getdate() value from the OS of the server on which it runs. If your mail server has a different time zone setting, the time of the e-mails will be offset from the getdate() value in SQL Server. If all the code you describe runs in the same batch or on the same connection, you won't get any data back from the SELECT at the end until the INSERT and UPDATE statements are committed, so they must be finished when the e-mail process gets the rows from the SELECT.
P.S. - You would see the offsets you describe if your mail server uses the local time for either the US Mountain or US Pacific time zone and your SQL Server uses UTC time (depending on daylight savings settings).