Click here to Skip to main content
13,097,012 members (67,843 online)
Rate this:
Please Sign up or sign in to vote.
Hi Friends:

I have a "Product" table in which I want to create a new columns "Modified_By" and "Modified_Date_Time". This columns does not allow nulls.

However, as the database already has data, in order to create this column I had to defined as "allowing nulls". Then, I run a process which updated the new column. The last step was to uncheck the "Allow nulls" property, but when I tried to save the table changes, I got the following error:

'Product_Details' table - Unable to modify table.
Cannot insert the value NULL into column 'Modified_Date_Time', table 'Vendor Products.dbo.Tmp_Product_Details'; column does not allow nulls. INSERT fails. The statement has been terminated.

All the rows were succesfully updated with the correct value in the "Modified_By" and "Modified_Date_Time" column, so I don't know why I get this error...Anyway, it seems like a new "temporary" table was created by SQL Server 2008, because I don't have any table with the name "Tmp_Orders"

Any help is appreciated..Thanks :)
Posted 11-Dec-12 5:18am
Updated 11-Dec-12 6:00am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try setting a default value for the column when you turn off accepting nulls. That should allow you to change it.
Joy1979 11-Dec-12 11:38am
Hi..Thx for your reply. I tried with this query but it gave me column with -1 as data in it.

Alter Table Product_Details
Add Modified_By varchar (50) NOT NULL default -1

I tried other query which is not allowing me to add any of column.

ALTER TABLE Product_Details ALTER COLUMN Modified_By varchar (50) NOT NULL

..Please advise.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

When I've had to do this in the past, I've done it in a multi-step process. Mostly because it's just easy to validate each step of the process and it's easy to do. (KISS principle)

1) Create your new columns will NULL allowed initially.
2) Update your data to have valid values in these columns.
3) Alter the tables to make the columns NOT NULL.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100