Pregunta

I have a table in SQL server 2008. I need a column that until now wasn't necessary, not to allow NULL anymore, without droping the table. I tried to do something like this:

ALTER TABLE [Sessions] ALTER COLUMN region_id NOT NULL;

EDIT: It was a small syntax error. solved.

¿Fue útil?

Solución 2

you are missing datatype.

ALTER TABLE [Sessions] ALTER COLUMN region_id int NOT NULL;

Otros consejos

You have to specify the data type as well when you alter a column:

ALTER TABLE [Sessions] ALTER COLUMN region_id int /* ? */ NOT NULL;

You have to first set all values that are NULL to a non NULL value:

UPDATE [Sessions] 
SET region_id=-1
WHERE region_id IS NULL

Instead of -1 you should choose something that would represent the formerly NULL values so you may distinguish them.

To get away with saving prevention you may go on:

Tools>Options>Designers> and unclick Prevent Saving Changes that require table re-creation

First off to save table chances like this you need to either script the changes, or when saving from the UI, you need to set SSMS to drop and recreate table for you. To do this;

Go to Tools Options Designers UNTICK prevent saving changes that require table re-creations.

If you script the changes, you will need to first alter the table, and add a new column allowing nulls. Then update the column and remove nulls and then after you will be able to set the column to NOT NULL.

If you follow these steps you will solve your issue.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top