Click here to Skip to main content
16,016,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 11-Dec-12 6:00am
v2

Try setting a default value for the column when you turn off accepting nulls. That should allow you to change it.
 
Share this answer
 
Comments
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.
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.
 
Share this answer
 
v2
Comments
Member 11969558 10-Oct-19 2:06am    
Thank you very much "fjdiewornncalwe". It's work for me.
Very fine and Thanks in Advance.

Regards . . .

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900