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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:06:57+00:00 2026-05-12T05:06:57+00:00

I want to alter a field from a table which has about 4 million

  • 0

I want to alter a field from a table which has about 4 million records. I ensured that all of these fields values are NOT NULL and want to ALTER this field to NOT NULL

ALTER TABLE dbo.MyTable
ALTER COLUMN myColumn int NOT NULL

… seems to take forever to do this update. Any ways to speed it up or am I stuck just doing it overnight during off-hours?

Also could this cause a table lock?

  • 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-12T05:06:57+00:00Added an answer on May 12, 2026 at 5:06 am

    You can alter a field and make it not null without it checking the fields. If you are really concerned about not doing it off hours you can add a constraint to the field which checks to make sure it isn’t null instead. This will allow you to use the with no check option, and not have it check each of the 4 million rows to see if it updates.

    CREATE TABLE Test
    (
        T0 INT Not NULL,
        T1 INT NUll 
    )
    
    INSERT INTO Test VALUES(1, NULL) -- Works!
    
    ALTER TABLE Test
        WITH NOCHECK
            ADD CONSTRAINT N_null_test CHECK (T1 IS NOT NULL)
    
        ALTER COLUMN T1 int NOT NULL 
    
    INSERT INTO Test VALUES(1, NULL) -- Doesn't work now!
    

    Really you have two options (added a third one see edit):

    1. Use the constraint which will prevent any new rows from being updated and leave the original ones unaltered.
    2. Update the rows which are null to something else and then apply the not null alter option. This really should be run in off hours, unless you don’t mind processes being locked out of the table.

    Depending on your specific scenario, either option might be better for you. I wouldn’t pick the option because you have to run it in off hours though. In the long run, the time you spend updating in the middle of the night will be well spent compared the headaches you’ll possibly face by taking a short cut to save a couple of hours.

    This all being said, if you are going to go with option two you can minimize the amount of work you do in off hours. Since you have to make sure you update the rows to not null before altering the column, you can write a cursor to slowly (relative to doing it all at once)

    1. Go through each row
    2. Check to see if it is null
    3. Update it appropriately.
      This will take a good while, but it won’t lock the whole table block other programs from accessing it. (Don’t forget the with(rowlock) table hint!)

    EDIT: I just thought of a third option:
    You can create a new table with the appropriate columns, and then export the data from the original table to the new one. When this is done, you can then drop the original table and change the name of the new one to be the old one. To do this you’ll have to disable the dependencies on the original and set them back up on the new one when you are done, but this process will greatly reduce the amount of work you have to do in the off hours. This is the same approach that sql server uses when you make column ordering changes to tables through the management studio. For this approach, I would do the insert in chunks to make sure that you don’t cause undo stress on the system and stop others from accessing it. Then on the off hours, you can drop the original, rename the second, and apply dependencies etc. You’ll still have some off hours work, but it will be minuscule compared to the other approach.

    Link to using sp_rename.

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

Sidebar

Related Questions

I have a table:'Categories' which has two fields:Category_ID and Category. Data in Category_ID field
I want to alter a table from INNODB to MEMORY ENGINE. So I typed
I want to alter a table if that column already exists. If it doesn't
Basically I want to alter the boolean value selecting from the table: e.g.: SELECT
I got a Table which has two fields: Point, and Level, with some sample
I have a table (it was imported from Excel) that has a wide variety
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have a table that I want : when the table has been updated,
I want to secure events stored in one table, which has relations to others.
i have a table which contains 10000 records, now i have added the field

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.