SQL Server:无限的 WHILE EXISTS 循环
我对以下 WHILE EXISTS 循环有问题。您能否考虑一下它是无限循环以及为什么它不更新值的原因是什么?
declare @part varchar(20)
while exists ((select top 1 * from part1 p where isnull(brojRacuna,'')=''))
begin
set @part=''
set @part=(select top 1 partija from part1 p where isnull(brojRacuna,'')='')
begin tran
update part1
set BrojRacuna= (select dbo.dev_brojracuna (@part))
where partija like @part
print @part
commit
end
编辑1:因为我在第一时间没有找到解决方案,所以我以这种方式创建了光标并更新了数据。之后,我发现剩下的几行未更新,因为函数存在数据问题并且无法更新该行的值。在这种情况下,字段始终为空,循环变得无限。
I have problem with the following WHILE EXISTS loop. Could you consider what can be reason why it is endless loop and why it doesn't update values?
declare @part varchar(20)
while exists ((select top 1 * from part1 p where isnull(brojRacuna,'')=''))
begin
set @part=''
set @part=(select top 1 partija from part1 p where isnull(brojRacuna,'')='')
begin tran
update part1
set BrojRacuna= (select dbo.dev_brojracuna (@part))
where partija like @part
print @part
commit
end
EDIT 1: Because I didn't find solution in first moment, I created cursor and updated data in that way. After that I found that left couple rows that are not updated, because function had an issue with data and couldn't update values for that rows. In that case, fields have been empty always and loop became endless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白为什么你选择partija值,因为你在where子句中有它,你可以通过这种方式简化很多:
顺便说一句,如果你有一个无限循环,也许函数dev_brojracuna不会返回正确的值值,并且 brojRacuna 保持不变。
I don't understand why you select the partija value, since you have it in the where clause, you can simplify a lot this way:
By the way, if you have an endless loop, maybe the function dev_brojracuna doesn't return the correct value, and brojRacuna remains unaltered.