Solved

Programmatically add default to column

Posted on 2009-05-19
6
180 Views
Last Modified: 2012-05-07
I have a table MyTable with a column MyColumn, which is defined as BIT and currently has no default value specified.

What SQL Syntax do I use to add a Default of 0 to that column?

I have tried ALTER TABLE MyTable ALTER COLUMN MyColumn DEFAULT 0;  which does not work.
0
Comment
Question by:wsturdev
6 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24422786
This should help you out:

ALTER TABLE MyTable ALTER COLUMN MyColumn SET DEFAULT 0;
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24422806
Hello wsturdev,

ALTER TABLE dbo.urTableb
ADD CONSTRAINT       DF_urConstraintName  DEFAULT 'DefaultValue' FOR ColumnName

Regards,

Aneesh
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 24422819
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24422852
If that didn't work then try creating a Default constraint as given below which would do the equivalent operation:

ALTER TABLE MyTable ADD CONSTRAINT def_constraint DEFAULT 0 FOR MyColumn;
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 24422931
hi wsturdev,

                Check this one.


http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20726441.html
ALTER TABLE MyTable ADD CONSTRANT  DF_ConstrantName DEFAULT 0 FOR MyColumn

Open in new window

0
 

Expert Comment

by:Lee R Liddick Jr
ID: 24897477
Sometimes you need to delete the prior default constraint that is automatically created by SQL Server...see this article:  http://bypsoft.blogspot.com/2007/10/changing-default-column-values-sql.html
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question