On long-running queries, which exact time is current_timestamp? I've got a query that checks for new patients in the ER, and if they have been there before in the last 72 hours, it notifies the killbo... uhhh... care managers.  

I could set this up as a trigger, but I don't think this is a good idea.  

What I want to do instead is set it up as a job that runs every 5 minutes, with this in the where clause:

    datediff(minute, TriageDateTime, current_timestamp) &lt;= 5


But the query itself takes 03:31.  

Question: is current_timestamp the timestamp of the beginning of the query?  

here's the entire big, bad query:

    with visits as(
    select LocationID, ab.RoomID, ab.UnitNumber as MRN, datediff(year,ab.BirthDateTime, CURRENT_TIMESTAMP) as age, 
    ed.TriageDateTime, ab.AdmitDateTime, 
    ab.ReasonForVisit as reason, ab.DischargeDateTime,  ab.PtStatus,
    lag(ed.TriageDateTime,1) over (partition by ab.UnitNumber order by ed.TriageDateTime) as last_TriageDateTime,
    lag(ab.ReasonForVisit,1) over (partition by ab.UnitNumber order by ed.TriageDateTime) as last_reason,
    lag(ab.DischargeDateTime,1) over (partition by ab.UnitNumber order by ab.AdmitDateTime) as Last_DischargeDateTime,
    lag(ab.PtStatus,1) over (partition by ab.UnitNumber order by ab.AdmitDateTime) as Last_PtStatus

    from 
    [eih-dr01].livefdb.dbo.EdmAcct_Main ed
    left join 
    [eih-dr01].livendb.dbo.AbstractData ab
    on ed.VisitID = ab.VisitID
    where 
    TriageDateTime &gt;= dateadd(hour, -100, current_timestamp)
    and 
    ab.UnitNumber is not null)


     select *, datediff(hour, last_TriageDateTime, TriageDateTime) as time_out 
     into #temp
     from visits 
     where datediff(hour, last_TriageDateTime, TriageDateTime) between 0 and 72
     and datediff(minute, TriageDateTime, current_timestamp) &lt;= 5


     declare @mrn varchar(55),
     @reasonforvisit varchar(255),
     @lasttriage varchar(255),
     @lastreasonforvisit varchar(255),
     @text nvarchar (max),
     @sql nvarchar (max),
     @subject varchar(255)

     declare c cursor local for
     select MRN, reason, cast(cast(last_TriageDateTime as date) as varchar(55)) + ' ' + convert(varchar(5), last_TriageDateTime, 108), last_reason
     from #temp

     open c
     fetch from c into @mrn, @reasonforvisit, @lasttriage, @lastreasonforvisit

     while @@FETCH_STATUS = 0
     begin

    set @text = 'Patient ' + @mrn + ' is in the ED for ' + isnull( @reasonforvisit, 'unkown reasons') + '. Their last visit was ' 
    + @lasttriage + ' for ' +  @lastreasonforvisit
    print @text

    set @subject = 'ED Return visit'

    EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'NoReply',
    @recipients = 
    '**********@vtext.com;',

    @subject = @subject,
    @execute_query_database = 'Master',
    @body = @text,
    @body_format = 'html'


    fetch next from c into @mrn, @reasonforvisit, @lasttriage, @lastreasonforvisit
     end
     close c
     deallocate c
     select * from #temp
     drop table #temp
It does move over the duration of the query. If I toss in some

    print current_timestamp;


they change.

I'm going to work around it like this:

    declare @time datetime
    set @time = curent_timestamp