Remove default value on column in SQL table before setting a new one

Posted on 2007-09-29
Medium Priority
Last Modified: 2012-06-27
Hello Experts,

I am using dynamic SQL in the following SPROC to set the default value in a tables column. This works very nicely as long as there has not already been a defult value set.

This is the Error message coming from SQL Server when I run this SPROC on a table that already has a default value in the clomumn:

"Column already has a DEFAULT bound to it.\r\nCould not create constraint. See previous errors."}      System.Exception {System.Data.SqlClient.SqlException}

I can do this by creating and deleting temporary tables (Add the new default to a temp table, insert data into the temp table, copy date from temp table to main, drop and create temp  table etc) but this is cumbersome.

 Is there a way to delete the Default value first before running the SPROC to add a new one??



 @TripNum varchar(10),
 @TableName nvarchar(64),

--Dynamic SQL
      SET @TripNum = '2007B'
      SET @TableName  = 'dbo.ObsData'
      SET @SQL = 'ALTER TABLE ' + @TableName + ' WITH NOCHECK ADD DEFAULT ''' +  @TripNum + ''' FOR TripNum'


Question by:Saxitalis
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 19984826
see here how to identify and drop an existing default on a column:

Author Comment

ID: 19985019
Got it - Much cleaner than how I was going about it...


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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

850 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