Solved

Programmatically add default to column

Posted on 2009-05-19
6
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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