views:

56

answers:

3

How to change one attribute in a table using T-SQL to allow nulls (not null --> null)? Alter table maybe?

+1  A: 

ALTER TABLE is right:

ALTER TABLE MyCustomers ALTER COLUMN CompanyName VARCHAR(20) NULL
Oded
You need to specify the column's type too.
LukeH
@LukeH - quite right. If you read the description _after_ the example, you will see I noted that. Answer updated for a more accurate example.
Oded
@Oded: Are you sure you need to re-define the constraints, and just the data type? [The MSDN article](http://msdn.microsoft.com/en-us/library/ms190273.aspx) doesn't mention that constraints would need to be redefined: "If NULL or NOT NULL is specified with ALTER COLUMN, new_data_type [(precision [, scale ])] must also be specified. If the data type, precision, and scale are not changed, specify the current column values."
Daniel Vassallo
@Daniel Vassallo - You are right. I was trying to be complete, but changing NULL/NOT NULL should be the only change.
Oded
+1  A: 

Yes you can use ALTER TABLE as follows:

ALTER TABLE [table name] ALTER COLUMN [column name] [data type] NULL

Quoting from the ALTER TABLE documentation:

NULL can be specified in ALTER COLUMN to force a NOT NULL column to allow null values, except for columns in PRIMARY KEY constraints.

Daniel Vassallo
+2  A: 
-- replace NVARCHAR(42) with the actual type of your column
ALTER TABLE your_table
ALTER COLUMN your_column NVARCHAR(42) NULL
LukeH