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
Joy1979269
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 490
1 Sergey Alexandrovich Kryukov 325
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web03 | 2.8.150331.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