0

好吧,我完全不知所措——这段代码曾经可以工作,现在突然不行了......

    declare GetAllCodes cursor local for 
    select ac.activationCodePKID from ActivationCodes ac
    left join OrdersLineItems od on ac.activationCodePKID = od.activationCodePKID 
    where od.activationCodePKID is null and ac.internalorderPKID is not null
    and ac.campaignID is not null
    and ac.enteredBy like 'ProcessRequest|Entitlement%'

    RAISERROR ('step completed', 0, 1) WITH NOWAIT 


     open GetAllCodes 
           while (1=1)
           begin             

             RAISERROR ('entering cursor', 0, 1) WITH NOWAIT 

                  fetch next from GetAllCodes into @activationCodePKID

                  if (@@fetch_status <> 0)
                  begin

                         DEALLOCATE GetAllCodes 
                         break

                  end


             exec fixOrder @activationCodePKID, 6, 7

          end

它似乎只是陷入了一个循环......我已经注释掉了 exec 语句,并且只是放入了一个 print 语句,但它一直在继续。“步骤完成”打印语句被打印,“进入光标”语句也是如此。然后什么都没有....只是挂起。查询本身返回 192 行,所以要循环,它应该循环 192 次,然后中断并结束。想法?

编辑:

我添加了这个:


declare @var varchar(10)
set @var = 'here: ' + cast(@@fetch_status as varchar(10))
RAISERROR (@var, 0, 1) WITH NOWAIT 

...就在fetch next from GetAllCodes into @activationCodePKID声明之后 - 仍然没有。'进入光标'仍然被打印,但它只是挂起......

编辑2:

我删除了很多东西,并在'declare cursor'语句之后添加了这个,看看我是否可以输出任何东西......


RAISERROR ('query done', 0, 1) WITH NOWAIT 
open GetAllCodes 
fetch next from GetAllCodes into @activationCodePKID  
close GetAllCodes 
deallocate GetAllCodes 

仍然挂起......所以我拿出了'fetch statement',它似乎不再挂起。显然,它没有做任何事情,因为我没有让它做任何事情,但它完成了执行。

这让我想到了为什么 fetch 语句挂起。是否有一些服务器设置可能会影响这一点?一些内存问题?硬件问题?

4

2 回答 2

4

光标的结构是这样的有原因吗?

通常游标是这样构建的:

declare @dbName varchar(max);
declare myCursor cursor for select [name] from sys.databases;
open myCursor;
fetch next from myCursor into @dbName;

while (@@fetch_status = 0)
begin
  print @dbName;
  fetch next from myCursor into @dbName;
end;

close myCursor;
deallocate myCursor;

每当他们陷入困境时,将是因为获取状态未设置为零(对我来说通常原因是缺少“获取下一个”)。

--

编辑:您可以检查活动监视器并查看那段 SQL 是否正在运行?有没有死锁?

于 2011-06-20T22:12:24.427 回答
0

我的猜测是,@@fetch_status 返回的不是 0,例如 -1 或 -2

MSDN 链接

也许检查一下那里到底有什么;这应该给你一个关于什么是错的线索。

于 2011-06-20T22:11:48.160 回答