事务内循环内的每次迭代是否都会重新评估 GetDate()?
我有一个场景,我在事务中循环结果集,并且需要在结果集中的每次迭代的表中插入唯一的日期时间值 - 每次都会重新计算 GetDate() 还是仅在第一次计算然后循环中的每次迭代都相同?
我的伪代码如下:
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
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是。
如果您想避免重新计算它,请将其值存储在循环之前的变量中,然后将其插入。
Yes.
If you want to avoid re-evaluating it, store its value in a variable before the loop, and insert that instead.