Saturday, December 8, 2012

How to Add two columns in Sql Server

How to Add two columns in existing table in Sql Server with default values


SQL-Server allows to add null value columns to an existing table. not null value columns also can be added if table is empty. If data exists, truncate/copy to some other table, then add not null value columns.

Here is the example for adding 2 columns in exiting table in sql server 2012


ALTER TABLE CUSTOMERS ADD [JOINED ON] DATETIME default getutcdate(),
status bit default 0;

The above alter statement add 2 columns to an existing table with default values utc date and status is false.


tags:How to Add two columns in Sql Server with default column constraint to table,How to Add columns to existing table in Sql Server ,How to Add new columns in existing table in Sql Server,sql-server 2012, SQL-Server:How to add column with default constraint values, How to add datetime column to table









ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'firstname' cannot be added to non-empty table 'cUSTOMERS' because it does not satisfy these conditions.









How to Add two columns in Sql Server with default column constraint to table,How to Add columns to existing table in Sql Server ,How to Add new columns in existing table in Sql Server,sql-server 2012, SQL-Server:How to add column with default constraint values

No comments:

Post a Comment