Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Infinite loop

Posted on 2013-12-13
6
292 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

837 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