Email
About
Archives
Stuff
how2: Add a default value to an existing column using T-SQL
growled on Friday, December 09, 2005 5:37:50 PM (Pacific Standard Time, UTC-08:00)
barked at
code [t-sql]
How to add a default value (constraint) to an existing column in SQL using transact SQL (T-sql).
I had an issue recently where rows were not being inserted into one of our SQL tables and I figured out that it was because one of the columns does not allow null values [for good reason]. When originally designed we did not expect the particular field to be submitted without a value, but now we have encountered just such a scenario. Instead of allowing null values in the column we decided it would be more appropriate to have a default value of "None."
We install everything via scripts so that means for me to roll out the fix to production I had to code the update in transact SQL. Add to that the fact that I cannot lose any data from the table made the process a little more interesting than I had expected. I can do this through the GUI in 10 seconds flat or via T-SQL when first creating the table column, but I've never had to do it to an existing table via T-SQL. Let me tell you...I had a hell of a time trying to figure out the syntax from SQL Books Online. In fact, I didn't.
My new peer, Shashi, found how buried in a comment to
this post
dated 11.7.2000. Go figure. [shrug]
Here's the exact syntax:
ALTER
TABLE
TableName_Here
WITH
NOCHECK
ADD
CONSTRAINT
[
ConstraintName
]
DEFAULT
'DefaultValue_Here'
FOR
ColumnName_Here
In our environment our installation scripts are run each time we put out a new or updated build which means that we have to make changes without upsetting current data. That also means that we have to check for the existence (or lack) of something before making changes. So here's the context in which I scripted it:
First I check to see if the table already has a default constraint on any of it's columns. Note that this will return 1 if ANY column has a default value set! In my case, I know that this is the only column in this table that will have a default value. If no default constraint is found (returning 0) then I run the ALTER TABLE line, otherwise I don't take any action.
IF
OBJECTPROPERTY
(
OBJECT_ID
(
'TableName_Here'
)
,
'TableHasDefaultCnst'
)
=
0
BEGIN
ALTER
TABLE
TableName_Here
WITH
NOCHECK
ADD
CONSTRAINT
[
ConstraintName
]
DEFAULT
'DefaultValue_Here'
FOR
ColumnName_Here
END
~
tod
tags:
sql
transact+sql
Comments [4]
Related posts:
Happy New Year (and oh, by the way, the sh*t's broke again)
how2: Concatenate multiple rows into a string in SQL
Tracked by:
"http://www.google.com/search?q=rbpuvuzi" (http://www.google.com/search?q=rbpuvu...
[Pingback]
"http://www.winthrop.org/newsroom/archives/release.cfm?id=363" (http://www.winth...
[Pingback]
"http://www.pharmiweb.com/PressReleases/pressrel.asp?ROW_ID=3262" (http://www.ph...
[Pingback]
"http://www.pharmiweb.com/PressReleases/pressrel.asp?ROW_ID=3253" (http://www.ph...
[Pingback]
"http://www.jugend.essen.de/module/bands/BandMaske.asp?BID=257229" (http://www.j...
[Pingback]
"http://www.ideamarketers.com/library/profile.cfm?writerid=42883" (http://www.id...
[Pingback]
"http://archive.slamdance.com/2006/festival/film_detail.asp?film_id=1312" (http:...
[Pingback]
"http://www.internshipprograms.com/CompanyDetail.asp?CompanyID=34533" (http://ww...
[Pingback]
"http://www.google.com/search?q=vikxtdyb" (http://www.google.com/search?q=vikxtd...
[Pingback]
"http://www.google.com/search?q=hogabntp" (http://www.google.com/search?q=hogabn...
[Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=180" (http://clutchfans.net/feature...
[Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=197" (http://clutchfans.net/feature...
[Pingback]
"http://www.engcen.com/EmpProfile.asp?EmployerID=5960" (http://www.engcen.com/Em...
[Pingback]
"http://www.engcen.com/EmpProfile.asp?EmployerID=5997" (http://www.engcen.com/Em...
[Pingback]
"http://www.seriea.tv/NewsDetails.asp?articolo=976" (http://www.seriea.tv/NewsDe...
[Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=172" (http://clutchfans.net/feature...
[Pingback]
"http://www.southafrica.net/satourism/media/newsreleasedetail.cfm?NewsReleaseID=...
[Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=194" (http://clutchfans.net/feature...
[Pingback]
"http://www.southafrica.net/satourism/media/newsreleasedetail.cfm?NewsReleaseID=...
[Pingback]
"http://clutchfans.net/feature.cfm?FeatureID=176" (http://clutchfans.net/feature...
[Pingback]
"http://www.csp.state.co.us/press_releases.cfm?Article_ID=307" (http://www.csp.s...
[Pingback]
"http://www.csp.state.co.us/press_releases.cfm?Article_ID=321" (http://www.csp.s...
[Pingback]
"http://www.landandfarm.com/lf/news/display_article.asp?cid=749" (http://www.lan...
[Pingback]
"http://www.edinburgh.gov.uk/environmentaldirectory/orgDetail.jsp?id=159" (http:...
[Pingback]
"http://www.epiphone.com/news.asp?NewsID=1111" (http://www.epiphone.com/news.asp...
[Pingback]
"http://www.anaheimoc.org/press/pressNews.asp?pid=127" (http://www.anaheimoc.org...
[Pingback]
"http://www.destinationcrm.com/articles/default.asp?ArticleID=7577" (http://www....
[Pingback]
"http://www.edinburgh.gov.uk/environmentaldirectory/orgDetail.jsp?id=162" (http:...
[Pingback]
"http://www.anaheimoc.org/press/pressNews.asp?pid=128" (http://www.anaheimoc.org...
[Pingback]
"http://www.se.pg.com/view.aspx?id=192" (http://www.se.pg.com/view.aspx?id=192)
[Pingback]
"http://www.epiphone.com/news.asp?NewsID=1102" (http://www.epiphone.com/news.asp...
[Pingback]
"http://www.destinationcrm.com/articles/default.asp?ArticleID=7579" (http://www....
[Pingback]
"http://www.se.pg.com/view.aspx?id=196" (http://www.se.pg.com/view.aspx?id=196)
[Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2075" (http://ww...
[Pingback]
"http://asp1.umbc.edu/newmedia/studio/stream/qtdetail.cfm?recordID=585" (http://...
[Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32633" (http://www.colle...
[Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=225" (http://www.myanmarmp3.net/...
[Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent...
[Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2096" (http://ww...
[Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=228" (http://www.myanmarmp3.net/...
[Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent...
[Pingback]
"http://www.telepacific.com/aboutTelePacific/press/press.asp?id=2097" (http://ww...
[Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent...
[Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=232" (http://www.myanmarmp3.net/...
[Pingback]
"http://asp1.umbc.edu/newmedia/studio/stream/qtdetail.cfm?recordID=578" (http://...
[Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent...
[Pingback]
"http://www.collegetennisonline.com/view/news.aspx?nwId=32636" (http://www.colle...
[Pingback]
"http://www.myanmarmp3.net/readnews.aspx?NewsID=216" (http://www.myanmarmp3.net/...
[Pingback]
"http://lakeline.lib.fl.us/news_and_events/calendar/lclseventdetail.aspx?IDEvent...
[Pingback]
"http://yesihavemoneyy.com" (http://yesihavemoneyy.com)
[Pingback]
"http://tubepornoss.com" (http://tubepornoss.com)
[Pingback]
"http://realvideopornoo.com" (http://realvideopornoo.com)
[Pingback]
Tuesday, November 21, 2006 10:18:12 AM (Pacific Standard Time, UTC-08:00)
This is just what I needed ... Thanks!
Montreal
Tuesday, November 21, 2006 12:42:52 PM (Pacific Standard Time, UTC-08:00)
Glad I could help!
tod
Friday, July 20, 2007 5:29:55 AM (Pacific Standard Time, UTC-08:00)
Thanx a lot! I used it twice yet :)
Norren
Thursday, July 26, 2007 7:42:54 AM (Pacific Standard Time, UTC-08:00)
Thanks!
Martin
Martin
Comments are closed.
Categories
blog swap
books [non-technology]
books [technology]
code [.net]
code [t-sql]
code [vbscript]
coding
dogs
funnies
links
microsoft
one liners
parenthood
ramblings
sports
technology
testing
video games
workplace
Subscribe
Email
or
RSS 1.0, RSS 2.0 & Atom
Archives
May, 2008 (2)
April, 2008 (6)
March, 2008 (8)
February, 2008 (11)
January, 2008 (10)
December, 2007 (6)
November, 2007 (6)
October, 2007 (2)
September, 2007 (3)
July, 2007 (3)
June, 2007 (7)
May, 2007 (11)
April, 2007 (7)
March, 2007 (8)
February, 2007 (11)
January, 2007 (1)
December, 2006 (4)
November, 2006 (6)
October, 2006 (12)
September, 2006 (15)
August, 2006 (23)
July, 2006 (18)
June, 2006 (7)
May, 2006 (9)
April, 2006 (4)
March, 2006 (11)
February, 2006 (12)
January, 2006 (5)
December, 2005 (15)
November, 2005 (7)
October, 2005 (13)
September, 2005 (15)
August, 2005 (6)
July, 2005 (21)
June, 2005 (14)
May, 2005 (26)
April, 2005 (25)
March, 2005 (4)
February, 2005 (20)
January, 2005 (6)
December, 2004 (4)