SQL Server:无限的 WHILE EXISTS 循环

发布于 2024-12-07 11:22:13 字数 536 浏览 0 评论 0原文

我对以下 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 技术交流群。

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

发布评论

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

评论(1

书信已泛黄 2024-12-14 11:22:13

我不明白为什么你选择partija值,因为你在where子句中有它,你可以通过这种方式简化很多:

declare @part varchar(20)

while exists ((select 1 from part1 p where isnull(brojRacuna,'')='' and partija='1111'))
begin
begin tran
update part1
set  BrojRacuna= (select dbo.dev_brojracuna ('1111'))
where partija like '1111'
commit
end

顺便说一句,如果你有一个无限循环,也许函数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:

declare @part varchar(20)

while exists ((select 1 from part1 p where isnull(brojRacuna,'')='' and partija='1111'))
begin
begin tran
update part1
set  BrojRacuna= (select dbo.dev_brojracuna ('1111'))
where partija like '1111'
commit
end

By the way, if you have an endless loop, maybe the function dev_brojracuna doesn't return the correct value, and brojRacuna remains unaltered.

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