Running UPDATE statement inside a WHILE loop in SQL takes forever to run
I have table which contains about 10 Million records. I am trying to update this table inside a while Loop in 1000 chunks as below: DECLARE @totalRecordCount INT DECLARE @midCount INT DECLARE @Chunksize INT set @totalRecordCount = (SELECT COUNT(*) FROM Tabletemp) set @ChunkSize = CEILING(@totalRecordCount/1000)+1 -- Divide the total into 1000 chunks set @midcount = @totalRecordcount while @midcount > 0 BEGIN UPDATE top(@Chunksize) a set a.x = b.x FROM TableTemp a INNER JOIN TableTemp2 b on a.id = b.id WHERE a.x is Null SET @midCount=@@ROWCOUNT END I tried putting a select statement before @@RO
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.