Does GetDate() get re-evaluated for each iteration within loop within a transaction?

天大地大妈咪最大 提交于 2019-12-24 00:43:41

问题


I have a scenario where I am looping through a resultset within a transaction and I need to INSERT a unique datetime value within a table for each iteration through the resultset - will GetDate() be recalculated each time or will it only be calculated the first time and then be the same for each iteration through the loop?

My pseudo-code is below:

BEGIN TRANSACTION
GO

DECLARE @ID INT 
DECLARE @table TABLE (/* Columns */) 

WHILE (SELECT COUNT(*) FROM @table WHERE PROCESSED = 0) > 0
      BEGIN

            SELECT TOP 1 @ID = ID FROM @table WHERE PROCESSED = 0 

            -- INSERT GetDate() into child table at this point. 
            -- Will GetDate() be re-evaluated each time? 

            UPDATE @table SET PROCESSED = 1 WHERE ID = @ID 

      END

END TRANSACTION
GO

Thanks in advance!


回答1:


Yes.

If you want to avoid re-evaluating it, store its value in a variable before the loop, and insert that instead.



来源:https://stackoverflow.com/questions/1847720/does-getdate-get-re-evaluated-for-each-iteration-within-loop-within-a-transact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!