I understand from sources like this that GETDATE()
should always (well, eventually, depending on how fast the loop is) return a different value in a loop.
I have this TSQL:
DECLARE @WaitUntilTime DATETIME = DATEADD(SECOND, 10, GETDATE())
WHILE (DATEDIFF(SECOND, GETDATE(), @WaitUntilTime) > 0)
BEGIN
SELECT GETDATE(), @WaitUntilTime
END
But it's always outputting the same value for GETDATE()
, and the loop never ends after 10 seconds like I want it to. Why?
I also found this answer which sounds like a similar case, but it talks about GETDATE()
being evaluated once for a column in a SELECT
query. I don't know if that applies to a while-loop as well.
By the way, I know about WAITFOR
but I can't use it because I want to introduce a delay within a single batch.