Loop and update statement in batches

Posted on 2011-10-06
Last Modified: 2012-05-12
i would like to run this statement, 10,000 at a time, commit then get the next t0,000

update tbl1
set filed2 = 0

there are more than 100,000,000 rows, so i don't want to have one big commit statment, and fill up the transaction log. what is the best way to do this ?
Question by:basile
    1 Comment
    LVL 21

    Accepted Solution

    You want to use SET ROWCOUNT 10000 before your UPDATE statement.  Just loop through until there are no records where filed2 = 2.  Something like this.

    WHILE EXISTS(SELECT * FROM tbl1 WHERE filed2 <> 0)


    SET ROWCOUNT 10000

    UPDATE tbl1 SET filed2 = 0




    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    730 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

    15 Experts available now in Live!

    Get 1:1 Help Now