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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:33:04+00:00 2026-05-12T17:33:04+00:00

I am putting together a job on SQL Enterprise Manager 2000 to copy and

  • 0

I am putting together a job on SQL Enterprise Manager 2000 to copy and delete records in a couple database tables. We’ve run a straight up mass copy and delete stored procedure, but it could be running it on millions of rows, and therefore hangs the server. I was interested in trying to run the service in 100-ish record chunks at a time, so the server doesn’t grind to a halt (this is a live web database). I want this service to run once a night, which is why I’ve put it in an agent job. Is there any way to loop the calls to the stored procedures that actually do the copy and delete, and then “sleep” in between each call to give the server time to catch up? I know there is the WAITFOR command, but I’m unsure if this will hold the processor or let it run other queries in the meantime.

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-05-12T17:33:05+00:00Added an answer on May 12, 2026 at 5:33 pm

    “Chunkifying” your deletes is the preferred way to delete excessive amounts of data without bloating up transaction log files. BradC’s post is a reasonable example of this.

    Managing such loops is best done within a single stored procedure. To spread such work out over time, I’d still keep it in the procedure. Inserting a WAITFOR in the loop will put a “pause” between each set of deletes, if you deem that necessary to deal with possible concurrency issues. Use a SQL Agent job to determine when the procedure start–and if you need to make sure it stops by a certain time, work that into the loop as well.

    My spin on this code would be:

    --  NOTE: This is a code sample, I have not tested it
    CREATE PROCEDURE ArchiveData
    
        @StopBy DateTime
        --  Pass in a cutoff time.  If it runs this long, the procedure will stop.
    AS
    
    DECLARE @LastBatch  int
    
    SET @LastBatch = 1
    --  Initialized to make sure the loop runs at least once
    
    
    WHILE @LastBatch > 0
     BEGIN
    
        WAITFOR DELAY '00:00:02'
        --  Set this to your desired delay factor
    
        DELETE top 1000  --  Or however many per pass are desired
         from SourceTable
        --  Be sure to add a where clause if you don't want to delete everything!
    
        SET @LastBatch = @@rowcount
    
        IF getdate() > @StopBy
            SET @LastBatch = 0
    
     END
    
    RETURN 0
    

    Hmm. Rereading you post implies that you want to copy the data somewhere first before deleting it. To do that, I’d set up a temp table, and inside the loop first truncate the temp table, then copy in the primary keys of the TOP N items, insert into the “archive” table via a join to the temp table, then delete the source table also via a join to the temp table. (Just a bit more complex than a straight delete, isn’t it?)

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

Sidebar

Related Questions

I'm putting together a simple test database to learn MVC with. I want to
I'm putting together a database to manage image files located on a file server.
I am putting together a staff database and I need to be able to
I putting together a page that will display a set of stored values. I
I'm putting together my deployment plan for a major deployment next week (basically taking
I'm putting together a Swing application where I often want to replace the contents
I'm putting together a technical response to tender, one of the requirements is to
I'm putting together an xml schema for a simple xml (see bellow for both
I need some help putting together this query in Django. I've simplified the example
I'm putting together a very basic time-lapse sequence of images (about 120 images total).

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.