重置@@FETCH_STATUS存储过程

发布于 2024-07-06 21:04:08 字数 97 浏览 8 评论 0原文

如何在存储过程中重置 @@FETCH_STATUS 变量或将其设置为 0?

另外,您可以将 FETCH_STATUS 绑定到特定游标吗?

How can I reset the @@FETCH_STATUS variable or set it to 0 in a stored procedure?

Also, can you bind FETCH_STATUS to a particular cursor?

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

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

发布评论

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

评论(8

辞旧 2024-07-13 21:06:27

我认为需要重置光标。
我正在使用游标进行一些测试,并且我不断返回 @@fetch_status = -1,即使在我对正在测试的游标编写了关闭和释放代码之后也是如此。

发生的情况是,我在逻辑测试后在另一个过程中打开了一个全局游标,并且在迭代它之后从未关闭该游标。

所以 Fetch 看到了该游标的 @@Fetch_status。

关闭光标,释放光标。

I had what I thought was a need to reset my cursor.
I was doing some testing with cursors and I kept coming back with a @@fetch_status being = -1, even after I had coded a close and deallocation to the cursor I was testing.

What had happened was I had opened a global cursor in another procedure after a logical test and never closed that cursor after iterating through it.

So the Fetch was seeing the @@Fetch_status of that cursor.

Close your cursors, deallocate your cursors.

無處可尋 2024-07-13 21:06:20

如果你想打破光标你可以使用BREAK
但这只是将函数从0替换为1。

fetch next
While @@fetch_Status = 0
begin

if (my condition)
 break
fetch next ;

end 

If you want to break cursor You can use BREAK
But this only only replace function from 0 to 1.

fetch next
While @@fetch_Status = 0
begin

if (my condition)
 break
fetch next ;

end 
仙气飘飘 2024-07-13 21:06:13

通常,在 FETCH 之后立即会出现 @@FETCH_STATUS,那么为什么要重置它呢?

如果不立即求值,请尝试将其结果存储在临时变量中。

Typically you have a @@FETCH_STATUS immediately after a FETCH, so why would you want to reset it?

Try to store its result in a temporary variable if you do not evaluate immediately.

寄居人 2024-07-13 21:06:05

你不能:

@@FETCH_STATUS (Transact-SQL)

返回最后一个的状态
游标
FETCH 语句发出
针对当前打开的任何光标

通过连接。

所以基本上它不绑定到任何光标。

You can't:

@@FETCH_STATUS (Transact-SQL)

Returns the status of the last
cursor
FETCH statement issued
against any cursor
currently opened
by the connection.

So basically it's not bound to any cursor.

她说她爱他 2024-07-13 21:05:56

我知道一个旧线程,但在其他地方找到的对我有用的答案是:

WHILE (1 = 1) 
BEGIN
    FETCH NEXT FROM mycursor INTO @somevar
    IF (@@FETCH_STATUS <> 0) BREAK
    -- do stuff here
END

An old thread I know but an answer found elsewhere that worked for me was:

WHILE (1 = 1) 
BEGIN
    FETCH NEXT FROM mycursor INTO @somevar
    IF (@@FETCH_STATUS <> 0) BREAK
    -- do stuff here
END
小姐丶请自重 2024-07-13 21:05:46

您需要关闭光标,然后再次打开光标。

DECLARE @IDs int
DECLARE MyCursor CURSOR FOR(SELECT ID FROM Table)
OPEN MyCursor
FETCH NEXT FROM MyCursor INTO @IDs
WHILE @@FETCH_STATUS=0
BEGIN
    --Do your work(First loop)
FETCH NEXT FROM MyCursor INTO @IDs
END
CLOSE MyCursor
--Run the cursor again
OPEN MyCursor
FETCH NEXT FROM MyCursorINTO @IDs
WHILE @@FETCH_STATUS=0
BEGIN
    --Other work (Second loop)
    FETCH NEXT FROM MyCursor INTO @IDs
END
CLOSE MyCursor
DEALLOCATE MyCursor

You need to close the cursor and then Open the cursor again.

DECLARE @IDs int
DECLARE MyCursor CURSOR FOR(SELECT ID FROM Table)
OPEN MyCursor
FETCH NEXT FROM MyCursor INTO @IDs
WHILE @@FETCH_STATUS=0
BEGIN
    --Do your work(First loop)
FETCH NEXT FROM MyCursor INTO @IDs
END
CLOSE MyCursor
--Run the cursor again
OPEN MyCursor
FETCH NEXT FROM MyCursorINTO @IDs
WHILE @@FETCH_STATUS=0
BEGIN
    --Other work (Second loop)
    FETCH NEXT FROM MyCursor INTO @IDs
END
CLOSE MyCursor
DEALLOCATE MyCursor
赢得她心 2024-07-13 21:05:34

您可以通过读取不在表末尾的游标来重置它。

You can reset it by reading a cursor which is not at the end of a table.

美人骨 2024-07-13 21:05:19

我能够重现 @@FETCH_STATUS 您描述的问题,这是一旦您DECLARE一个CURSOR并通过调用FETCH NEXT迭代行,直到您的@@FETCH_STATUS = -1
那么即使您CLOSEDEALLOCATE您的光标,如果您将该CURSOR调用回您的@@FETCH_STATUS = -1 并且如果您的循环条件基于 @@FETCH_STATUS <> -1,你的循环永远不会执行。

我的解决方案基本上是告诉 CURSOR 返回到 FIRST,将 @@FETCH_STATUS 更改回 0 ,然后退出。 请注意,在声明 CURSOR 名称后,必须通过在 CURSOR 名称后面添加关键字 SCROLL 来使 CURSOR 能够滚动。

这是一个例子。 我使用 orderitems(人们下订单的商品)表中的三列来创建游标:

USE transact_Sales;

GO

DECLARE @isOrderNumber INT;
DECLARE @isOrderTotal MONEY;
DECLARE test SCROLL CURSOR
FOR
SELECT Oi.order_num, SUM(Oi.quantity@item_price) FROM orderitems AS Oi GROUP BY order_num;

OPEN test;

WHILE @@FETCH_STATUS = 0
BEGIN 
FETCH NEXT FROM test INTO @isOrderNumber, @isOrderTotal
PRINT CAST(@isOrderNumber AS VARCHAR(20)) + ' '
     +CAST(@isOrderTotal AS VARCHAR(20)) + ' '
     +CAST(@@FETCH_STATUS AS VARCHAR(5))
END
FETCH FIRST FROM test INTO @isOrderNumber, @isOrderTotal

CLOSE test;
DEALLOCATE test;

这些是结果:

20005 149.87 0  
20006 55.00 0  
20007 1000.00 0  
20008 125.00 0  
20009 38.47 0  
20009 38.47 -1

游标可以反复运行,每次都会产生相同的结果。

I am able to reproduce the @@FETCH_STATUS issue you describe, this is once you DECLARE a CURSOR and iterate through the rows by calling FETCH NEXT until your @@FETCH_STATUS = -1.
Then even if you CLOSE and DEALLOCATE your cursor, if you call that CURSOR back your @@FETCH_STATUS = -1 and if your basing a loop condition on the @@FETCH_STATUS <> -1, your loop never executes.

My solution was to basically tell the CURSOR to go back to FIRST, changing the @@FETCH_STATUS back to 0, then exiting. Take note that one must enable to CURSOR to be scrolling by adding the keyword SCROLL after the CURSOR name when you declare it.

Here's an example. I used three columns from an orderitems (items people placed orders for) table to create my cursor:

USE transact_Sales;

GO

DECLARE @isOrderNumber INT;
DECLARE @isOrderTotal MONEY;
DECLARE test SCROLL CURSOR
FOR
SELECT Oi.order_num, SUM(Oi.quantity@item_price) FROM orderitems AS Oi GROUP BY order_num;

OPEN test;

WHILE @@FETCH_STATUS = 0
BEGIN 
FETCH NEXT FROM test INTO @isOrderNumber, @isOrderTotal
PRINT CAST(@isOrderNumber AS VARCHAR(20)) + ' '
     +CAST(@isOrderTotal AS VARCHAR(20)) + ' '
     +CAST(@@FETCH_STATUS AS VARCHAR(5))
END
FETCH FIRST FROM test INTO @isOrderNumber, @isOrderTotal

CLOSE test;
DEALLOCATE test;

These are the results:

20005 149.87 0  
20006 55.00 0  
20007 1000.00 0  
20008 125.00 0  
20009 38.47 0  
20009 38.47 -1

The cursor can be run over and over and will product the same results each time.

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