我正在尝试以块和事务的形式更新一个大表。
如果 column1 由于某种原因未更新,则此查询将无休止地运行。我在那里有另一个嵌套查询,它不一定返回一个值,所以一些 column1 在更新后保持为空。这使我的查询陷入无限循环。
如何指定“更新顶部”的开始位置?
提前致谢。
declare @counter int
declare @total int
declare @batch int
set @total = (SELECT COUNT(*) FROM table with(nolock))
set @counter = 0
set @batch = 1000
while (@counter < (@total/@batch) + 1)
begin
BEGIN TRANSACTION
set @counter = @counter + 1
Update TOP ( @batch ) table
SET column1 = 'something'
where column1 is null
Commit transaction
end