Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将游标替换为while循环但不工作_Sql_Sql Server - Fatal编程技术网

Sql 将游标替换为while循环但不工作

Sql 将游标替换为while循环但不工作,sql,sql-server,Sql,Sql Server,请帮帮我。 由于性能问题,我已将游标更改为While循环 但我的while循环不起作用,事实上它比通常需要更长的时间,而且它似乎是无限循环,请让我知道我在这里犯了什么错误 使用游标-工作良好 DECLARE policyVerify_cursor CURSOR LOCAL FOR Select AccountId, PolicyNumber, IsPolicyBalance, Remarks from @VerifyPolicyNumbers -- Open cursor OPEN polic

请帮帮我。 由于性能问题,我已将游标更改为While循环

但我的while循环不起作用,事实上它比通常需要更长的时间,而且它似乎是无限循环,请让我知道我在这里犯了什么错误

使用游标-工作良好

DECLARE policyVerify_cursor CURSOR LOCAL FOR
Select AccountId, PolicyNumber, IsPolicyBalance, Remarks from @VerifyPolicyNumbers

-- Open cursor
OPEN policyVerify_cursor

FETCH NEXT FROM policyVerify_cursor INTO @AccountId, @PolicyNumber, @IsPolicyBalance, @Remarks

WHILE (@@FETCH_STATUS= 0)
BEGIN

 -- my LOGIC
    FETCH NEXT FROM policyVerify_cursor
    INTO @AccountId, @PolicyNumber, @IsPolicyBalance, @Remarks

END 
我的查询使用While循环-不工作

DECLARE @NumberRecords int, @RowCounter int
SET @NumberRecords = (SELECT COUNT(*) FROM @VerifyPolicyNumbers)

SET @RowCounter = 1

-- loop through all records in the temporary table
-- using the WHILE loop construct
WHILE @RowCounter <= @NumberRecords
BEGIN
-- My Logic
SET @RowCounter = @RowCounter + 1
END

首先,我不知道@VerifyPolicyNumbers表和声明的游标来自何处。此外,如果@VerifyPolicyNumbers表是您在执行逻辑时添加的内容,那么这是否会导致某些问题。。。在您声明列表后还有更多记录吗

您可能希望将查询拉入一个临时会话表中,并使用该表进行查询,这样您就不会遇到我首先描述的可能场景。然后调整类似于

select
      AccountId, 
      PolicyNumber, 
      IsPolicyBalance, 
      Remarks 
   into
      #myTempList
   from 
      @VerifyPolicyNumbers


DECLARE policyVerify_cursor CURSOR 
-- declaring the cursor for SCROLLING THROUGH RECORDS
SCROLL FOR
Select 
      AccountId, 
      PolicyNumber, 
      IsPolicyBalance, 
      Remarks 
   from 
      #myTempList

-- Open cursor
OPEN policyVerify_cursor

-- explicitly select FIRST record
FETCH FIRST FROM policyVerify_cursor INTO 
   @AccountId, @PolicyNumber, @IsPolicyBalance, @Remarks

WHILE (@@FETCH_STATUS= 0)
BEGIN

   -- my LOGIC
   -- Now, get the NEXT record.
   FETCH NEXT FROM policyVerify_cursor
      INTO @AccountId, @PolicyNumber, @IsPolicyBalance, @Remarks

END 

CLOSE policyVerify_cursor
DEALLOCATE policyVerify_cursor

它可能会失败,因为您没有为SCROLL声明游标,也没有获取第一条记录以开始循环

当您将My Logic中的内容设置为空时会发生什么。循环结束得快吗?另外,我不知道为什么您的更改可能会提高性能,但我认为这对您现在的问题并不重要。感谢您的回答,我相信@NumberRecords count存在一些问题,您能告诉我如何解决吗。我的意思是,计数是以这种方式不断增加的,它将进入无限循环。这行有一些问题-从@VerifyPolicyNumbers中选择COUNT*,我在代码中没有看到任何暗示无限循环的内容。不管是什么错误,都与我的逻辑中发生的事情有关,你没有展示。我的逻辑代码太大了,它与当前使用游标的代码相同,工作正常,没有任何问题。但我用WHIEL循环改变了同样的东西,它将变成无限循环为什么你要用另一个光标来改变光标,而不是你自己写的?如果要提高性能,需要使用基于集合的操作。