事务内循环内的每次迭代是否都会重新评估 GetDate()?

发布于 2024-08-13 07:01:16 字数 591 浏览 8 评论 0原文

我有一个场景,我在事务中循环结果集,并且需要在结果集中的每次迭代的表中插入唯一的日期时间值 - 每次都会重新计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-08-20 07:01:16

是。

如果您想避免重新计算它,请将其值存储在循环之前的变量中,然后将其插入。

Yes.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文