?
Solved

Infinite loop

Posted on 2013-12-13
6
Medium Priority
?
304 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
[X]
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
6 Comments
 
LVL 11

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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 38

Accepted Solution

by:
Jim P. earned 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

752 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