t-sql 游标:“首先从”获取重新生成光标的内容?

发布于 2024-09-30 04:41:32 字数 433 浏览 2 评论 0原文

我有一段别人的代码,基本上是这样做的:

declare X cursor static for select * from stuff
open cursor X
fetch next from X
while @@fetch_status = 0
begin
  do stuff
  fetch next from X
end

fetch first from X
while @@fetch_status = 0
begin
  do different stuff
  fetch next from X
end

为了让它摆脱困境,是的,我知道游标不好/不再流行/等等。我没有写它。我只需要修复它。现在的问题是:

如果底层游标后面的数据在第一个循环和第二个循环之间发生变化,第二个游标循环是否会看到与第一个游标循环不同的数据?或者静态限定符会阻止这种情况吗?

I have a block of someone else's code that does essentially this:

declare X cursor static for select * from stuff
open cursor X
fetch next from X
while @@fetch_status = 0
begin
  do stuff
  fetch next from X
end

fetch first from X
while @@fetch_status = 0
begin
  do different stuff
  fetch next from X
end

To get it out of the way, yes, I know cursors are bad / no longer in fashion / etc. I didn't write it. I just have to fix it. Now to the question:

If the data behind the underlying cursor changes between the first loop and the second loop, could the second cursor loop see different data than the first cursor loop? Or does the static qualifier prevent that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

时光与爱终年不遇 2024-10-07 04:41:32

游标检索到的数据不会改变。如此处所述:

静态

定义一个光标
要使用的数据的临时副本
通过光标。所有请求均发送至
光标从这里回答
tempdb 中的临时表;所以,
对基表所做的修改是
没有反映在返回的数据中
对该游标进行获取,并且这
光标不允许修改。

The data retrieved by the cursor will not change. As documented here:

STATIC

Defines a cursor that makes a
temporary copy of the data to be used
by the cursor. All requests to the
cursor are answered from this
temporary table in tempdb; therefore,
modifications made to base tables are
not reflected in the data returned by
fetches made to this cursor, and this
cursor does not allow modifications.

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