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 7168635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:49:10+00:00 2026-05-28T14:49:10+00:00

I am looking at this query and I am kinda confused by a few

  • 0

I am looking at this query and I am kinda confused by a few things.

-- T-SQL large update table
USE tempdb;
SELECT * INTO SOD
FROM AdventureWorks2008.Sales.SalesOrderDetail
GO
--(121317 row(s) affected)

-- SQL update in batches of 10,000
WHILE (2 > 1)
  BEGIN
    BEGIN TRANSACTION
    UPDATE TOP ( 10000 ) SOD
    SET    UnitPriceDiscount = 0.08,
           ModifiedDate = CONVERT(DATETIME,CONVERT(CHAR(10),getdate(),112))
    WHERE  ModifiedDate < CONVERT(DATETIME,CONVERT(CHAR(10),getdate(),112))

    IF @@ROWCOUNT = 0
      BEGIN
        COMMIT TRANSACTION
         BREAK
      END
    COMMIT TRANSACTION
    -- 1 second delay
    WAITFOR DELAY '00:00:01'
  END -- WHILE
GO

/* Messages

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(10000 row(s) affected)

(1317 row(s) affected)

(0 row(s) affected)
*/
-- Cleanup
DROP TABLE SOD
GO

------------
  1. Is it making a temp table? SELECT * INTO SOD if so is this really needed? Can I just use the while part and below?
  2. How does IF @@ROWCOUNT = 0 ever become zero? Does it do some self incrementing or something?

Edit

This is what I have now but I still think there is an endless loop or something

BEGIN TRAN

declare
  @rows_updated int ,
  @rowCount int,
  @batch_size   int
  set @rows_updated = -1
  set @batch_size   = 10000
  set @rowCount = 0;

Declare @xx VARCHAR(20) DECLARE @length INT
SET @length = 17 SET @xx = 'XXXXXXXXXXXXXXXX'

while ( @rows_updated != 0 )
  begin
    update top(@batch_size)  myTbl
    SET myNumber = SUBSTRING(@xx, 0, @length - len(RIGHT(myNumber, 4))) + RIGHT(myNumber, 4) 
    WHERE myDate <'2011-Jan-02' 
    set @rows_updated = @@rowcount
   set @rowCount += 10000
    print @rowCount
  end

ROLLBACK

I did a count

select count(*) from myTbl
where  myDate < '2011-Jan-02' 

this brings back a count of 1,448,982

the last print out I got was 31,110,000

Edit 2

I added this and now it stops but it still is not 100% where it should be at

while (Select Count(*) From myTbl Where myDate  <'2011-Jan-02' ) >=  @rowCount

Edit 3

I think edit 2 is just doing the same 10,000 rows over and over again.

  • 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-05-28T14:49:11+00:00Added an answer on May 28, 2026 at 2:49 pm

    The provided script does the following:

    • Creates/Loads the permanent table SOD

    • while (true)

      • begins a transaction
      • updates at most 10,000 rows of the SOD table, where the modified date is less than the current date, setting the unit price discount to 0.08 and setting the modified date to the current date.
      • commits the transaction
      • if no rows were modified, exits the loop
      • repeat

    First, the begin transaction and commit transaction are unnecessary with a normal installation of SQL Server, unless you’ve explicitly changed the IMPLICIT_TRANSACTIONS setting by executing SET IMPLICIT_TRANSACTIONS ON, in which case the BEGIN TRANSACTION statement is unecessary. The reason is the by default, SQL Servers runs in auto-commit mode, so every statement fires, sotto voce, a commit on success or a rollback on failure.

    Also, the logic is a little clunky. I’d write the loop like this:

    declare
      @rows_updated int ,
      @batch_size   int
    
    set @rows_updated = -1
    set @batch_size   = 10000
    
    while ( @rows_updated != 0 )
      begin
    
        update top @batch_size SOD
        set UnitPriceDiscount = 0.08 ,
            ModifiedDate      = convert(datetime,convert(char(10),getdate(),112))
        where ModifiedDate    < convert(datetime,convert(char(10),getdate(),112))
    
        set @rows_updated = @@rowcount
    
      end
    

    which might make what’s going on a little more transparent.

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

Sidebar

Related Questions

I have a specific information in a table I am looking for, this query
Looking at similar questions, I actually want the exact opposite of this: SQL query
Looking to do a bit of refactoring... Using NHibernate I have this query currently
Is there any better way to make this query work? I'm looking for a
This is related to my previous thread: SQL Query takes about 10 - 20
Currently, I am using this query in my PHP script: SELECT * FROM `ebooks`
I am looking to perform the following query (in pseudo-code) on Android: SELECT C.ID,
I'm a noob at SQL things and I'm looking to do something here. I've
This is kinda theoretical question, I was looking at someone else' code (below) and
I have a rather complex SQL query that I am looking for a little

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.