Solved

Infinite loop

Posted on 2013-12-13
6
286 Views
Last Modified: 2013-12-14
I am stumped. Can anyone tell me why this code goes into an infite loop and just keeps running and running.  Using MSSQL.  The idea is that it updates rows, in chunks, of a very large table ~ approx 32M rows.  If I run this and stop it, the # of rows affected (when adding all the 250000 up) is greater than the total number of rows in the table.   Only a small fraction of them are actually updated then too as it turns out.


set nocount off
set rowCount 250000
declare @LastUpdated int
While 1=1 begin
  Begin Transaction

   Update [table] set [column1] = null where [column1] = 0

  SET @LastUpdated = @@rowCount
  COMMIT
 

  if @LastUpdated = 0 break
end
set rowCount 0

Pulling my hair out...
0
Comment
Question by:ccleebelt
6 Comments
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 39718121
What are you trying to change in the table? You can even use one Update statement to change Column1 (if that is the only thing you want to adjust). Or you can use a cursor to refer to the relevant rows and further change them. Read
Cursor Example
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39718189
The Transaction as written in quite unnecessary and the script could be made more efficient without using SET ROWCOUNT (which should be avoided as it will not longer be supported in future versions):
Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in a future release of SQL Server. Avoid using SET ROWCOUNT with DELETE, INSERT, and UPDATE statements in new development work, and plan to modify applications that currently use it. For a similar behavior, use the TOP syntax.

But to answer the question, assuming you know how many rows have column1 = 0 then perhaps there is a TRIGGER on the table.
0
 

Author Comment

by:ccleebelt
ID: 39718950
Thanks.  How would you recommend that I rewrite the query to adjust the data?  I have about 32 Million rows that need to be mass updated.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 38

Accepted Solution

by:
Jim P. earned 500 total points
ID: 39719215
Try this:
set nocount off

declare @LastUpdated as integer
While 1=1 begin
  Begin Transaction

   Update top (50000) [table] set [column1] = null where [column1] = 0

  SET @LastUpdated = @@rowCount
  COMMIT
 print @LastUpdated 

  if @LastUpdated = 0 break
end

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39719356
How would you recommend that I rewrite the query to adjust the data?
As shown in the accepted comment, but I would lose the Transaction as it is pointless and will only make it take longer.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 39719376
As shown in the accepted comment, but I would lose the Transaction as it is pointless and will only make it take longer.

I agree it adds time, but the significance, depending on the underlying system, isn't always there.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question