Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7918691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:35:45+00:00 2026-06-03T15:35:45+00:00

So, I came across some code that is supposed to help me to update

  • 0

So, I came across some code that is supposed to help me to update a massive table (100m+ records) by doing it in batches, as follows:

--Declare variable for row count
set rowcount 50000
go

Declare @rc int
Set @rc=50000

While @rc=50000
 Begin

  Begin Transaction

  --Use tablockx and holdlock to obtain and hold 
  --an immediate exclusive table lock. This unusually
  --speeds the update because only one lock is needed.

    update  MyTable With (tablockx, holdlock)
    set TestField='ABC'

    ----Get number of rows updated
    ----Process will continue until less than 50000
    select @rc=@@rowcount


  Commit
 End

Problem is, this goes into an infinite loop, priting (50000 row(s) affected) until the end of days. Incidentally, if the table has less than 50000 records the code above exits correctly. Anyone know how to fix this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:35:46+00:00Added an answer on June 3, 2026 at 3:35 pm

    if there are 50000 or more records in MyTable, then it will always update. You have no where clause in your update that would narrow the field to eventually getting you to <50000 records updated.

    Presumably you intended to only update records where TestField <> ‘ABC’ so you didn’t perform the same update again.

    When you’re thinking about what where clause to use, consider making use of the clustered index so you can perform an index seek/partial scan on your updates instead of a full table scan.

    Incidentally,There is no need for the HOLDLOCK hint since it is released as soon as the transaction commits, nor the explicit transaction since the update itself is a transaction. The tablock may yield a slight improvement since it avoids lock escalation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across some legacy code that is supposed to keep an incrementing sequence
Came across some code today that had a javascript array, made in php, using
I came across some code today that looks like this: subroutine sub(hello,world,this,routine,takes,a, & &
I came across some legacy code that contains a function like this: LPCTSTR returnString()
I came across some code recently that used a custom error handler to turn
I came across some code today that surprised me. A variable was defined (outside
I came across some Java code that has a method containing the following: static
I recently came across some Java code that simply put some strings into a
i have been going through some code and came across a statement that somehow
I recently came across some code that looks something like this: <head> <?php /*

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.