Solved

Infinite loop

Posted on 2013-12-13
6
277 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 38

Accepted Solution

by:
Jim P. earned 500 total points
Comment Utility
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
Comment Utility
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.
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 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

12 Experts available now in Live!

Get 1:1 Help Now