Solved

Programmatically add default to column

Posted on 2009-05-19
6
181 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
How our DevOps Teams Maximize Uptime

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

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query still returning duplicates 5 41
SQL Server 2012 r2 - Varible Table 3 31
Mysql Left Join Case 10 70
SQL Error - Query 6 41
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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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