[sql-server-2008] Add column to SQL Server

I need to add a column to my SQL Server table. Is it possible to do so without losing the data, I already have?

This question is related to sql-server-2008

The answer is


Add new column to Table

ALTER TABLE [table]
ADD Column1 Datatype

E.g

ALTER TABLE [test]
ADD ID Int

If User wants to make it auto incremented then

ALTER TABLE [test]
ADD ID Int IDENTITY(1,1) NOT NULL

Add new column to Table with default value.

ALTER TABLE NAME_OF_TABLE
ADD COLUMN_NAME datatype
DEFAULT DEFAULT_VALUE

Adding a column using SSMS or ALTER TABLE .. ADD will not drop any existing data.


Use this query:

ALTER TABLE tablename ADD columname DATATYPE(size);

And here is an example:

ALTER TABLE Customer ADD LastName VARCHAR(50);