Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Introduction:

In this article I will explain how to write Query to add new column with default value to existing datatable in SQL server.

Description:

In previous post I explained how to alter or add new column in datatable using SQL Server. Now I will explain how to write query to add new column with default value to existing datatable in SQL Server. During work with one application I got requirement like alter existing table structure to add new column with default value 0(zero). If we want to add new column with some default value to existing table we need to write the query in a format as shown below

Query to add new column with default value to existing table

If we want to add new column with default value to existing table that syntax will be like this


ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE NULL|NOT NULL CONSTRAINT CONSTRAINT_NAME DEFAULT DEFAULT_VALUE
Ex:


ALTER TABLE Employee_Details ADD isacitve INT NULL CONSTRAINT DF_TABLE_COL_ISACTIVE DEFAULT '0'
From the above query declaration new column “isacitve” will add to Employee_Details with data type INT and with Default value “0

In above query I declared CONSTRAINT_NAME without declare CONSTRAINT_NAME also we can add column with default value. If we declare CONSTRAINT_NAME it will take the name what we define otherwise automatically it will create constraint name that would be like this

Query to add new column with default value without constraint name


ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE NULL|NOT NULL DEFAULT DEFAULT_VALUE
Ex:


ALTER TABLE Employee_Details ADD isacitve INT NULL DEFAULT '0'

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.