Solved

Infinite loop

Posted on 2013-12-13
6
281 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now