Click here to Skip to main content
Rate this: bad
good
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 6:18am
Joy1979317
Edited 11-Dec-12 7:00am
v2
Rate this: bad
good
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.
  Permalink  
Comments
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
good
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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


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