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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:28:14+00:00 2026-05-17T00:28:14+00:00

Database : SQL Server 2005 Problem : Copy values from one column to another

  • 0

Database : SQL Server 2005

Problem : Copy values from one column to another column in the same table with a billion+
rows.

test_table (int id, bigint bigid)

Things tried 1: update query

update test_table set bigid = id 

fills up the transaction log and rolls back due to lack of transaction log space.

Tried 2 – a procedure on following lines

set nocount on
set rowcount = 500000
while @rowcount > 0
begin
 update test_table set bigid = id where bigid is null
 set @rowcount = @@rowcount
 set @rowupdated = @rowsupdated + @rowcount
end
print @rowsupdated

The above procedure starts slowing down as it proceeds.

Tried 3 – Creating a cursor for update.

generally discouraged in SQL Server documentation and this approach updates one row at a time which is too time consuming.

Is there an approach that can speed up the copying of values from one column to another. Basically I am looking for some ‘magic’ keyword or logic that will allow the update query to rip through the billion rows half a million at a time sequentially.

Any hints, pointers will be much appreciated.

  • 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-17T00:28:15+00:00Added an answer on May 17, 2026 at 12:28 am

    I’m going to guess that you are closing in on the 2.1billion limit of an INT datatype on an artificial key for a column. Yes, that’s a pain. Much easier to fix before the fact than after you’ve actually hit that limit and production is shut down while you are trying to fix it 🙂

    Anyway, several of the ideas here will work. Let’s talk about speed, efficiency, indexes, and log size, though.

    Log Growth

    The log blew up originally because it was trying to commit all 2b rows at once. The suggestions in other posts for "chunking it up" will work, but that may not totally resolve the log issue.

    If the database is in SIMPLE mode, you’ll be fine (the log will re-use itself after each batch). If the database is in FULL or BULK_LOGGED recovery mode, you’ll have to run log backups frequently during the running of your operation so that SQL can re-use the log space. This might mean increasing the frequency of the backups during this time, or just monitoring the log usage while running.

    Indexes and Speed

    ALL of the where bigid is null answers will slow down as the table is populated, because there is (presumably) no index on the new BIGID field. You could, (of course) just add an index on BIGID, but I’m not convinced that is the right answer.

    The key (pun intended) is my assumption that the original ID field is probably the primary key, or the clustered index, or both. In that case, lets take advantage of that fact, and do a variation of Jess’ idea:

    set @counter = 1
    while @counter < 2000000000 --or whatever
    begin
      update test_table set bigid = id 
      where id between @counter and (@counter + 499999) --BETWEEN is inclusive
      set @counter = @counter + 500000
    end
    

    This should be extremely fast, because of the existing indexes on ID.

    The ISNULL check really wasn’t necessary anyway, neither is my (-1) on the interval. If we duplicate some rows between calls, that’s not a big deal.

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

Sidebar

Related Questions

I am having a problem with one database on my SQL Server 2005 production
I'm thinking about porting a database from Sql Server 2005 to MySQL. I've become
I attached database from SQL Server 2000 to SQL Server 2005 and it worked
I've got a problem with multiple deadlocks on SQL server 2005. This one is
I have a problem inserting a datetime format variable to Sql Server 2005 database.
I have a table in a SQL Server 2005 Database that is used a
I have a table named t_Student in Microsoft SQL Server 2005 database. In that
I have a simple problem when querying the SQL Server 2005 database. I have
in my SQL Server 2005 database I have a column RMA_Number with datatype char(10)
I have a SQL Server 2005 database that is suffering from lock starvation because

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.