Click here to Skip to main content
11,717,126 members (81,016 online)
Rate this: bad
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 Smile | :)
Posted 11-Dec-12 5:18am
Edited 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 at 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
0 Sergey Alexandrovich Kryukov 659
1 F-ES Sitecore 260
2 Maciej Los 185
3 OriginalGriff 175
4 Richard MacCutchan 140
0 Sergey Alexandrovich Kryukov 1,029
1 Maciej Los 464
2 Richard MacCutchan 430
3 CHill60 415
4 OriginalGriff 415

Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
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