从游标源 SQL Server 中删除行
我有一个 SQL Server 2005 游标,对一个名为 @workingSet
的表变量进行操作。
有时行可能是相关的,在这种情况下,我同时处理已获取的行和相关行。然后,我从 @workingset
中删除相关记录,因为我不需要在循环中进行处理。
在具有 7 行的 @workingSet
中,前两行是相关的,因此当我处理 1 时,我也会处理 2。我从游标源 (@workingSet
) 中删除第 2 行,并且然后获取下一个。问题是它返回 @workingset 中的第二行(我在上一次迭代中删除的行)。
我的印象是,这可以完成,即从游标操作的源中删除一个项目,并且它将在后续的提取中遵循删除操作。
I have a SQL Server 2005 cursor operating over a table variable called @workingSet
.
Some times rows can be related and in this case I process the row I have fetched and the related rows at the same time. I then remove the related records from @workingset
as I don't need to process then in the loop.
In a @workingSet
with 7 rows, the first two are related so when I process 1 I also process 2. I remove row 2 from the cursor source (@workingSet
) and then fetch next. The problem is it returns the second row in @workingset
(the one I deleted on the previous iteration).
I was of the impression that this could be done i.e. deleting an item from a source that a cursor operates on and it will honour the delete in subsequent fetches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案似乎是用作游标源的表变量需要有一个主键。我已经添加了这个并且一切正常。
The answer appears to be that the table variable that is being used as the source of the cursor needs to have a primary key. I've added this and all works correctly.
对游标不太熟悉,但从快速测试来看,您需要避免使用 STATIC 或 KEYSET 选项声明游标,然后对基础表的更改会反映在光标。
Not massively familiar with cursors but from a quick test this end you need to avoid declaring the cursor with the
STATIC
orKEYSET
options then the changes to the underlying table are reflected in the cursor.