Sql server will not let you dropa table if it has default contraints (like default values for columns.)
This script will let you find them.
1. Determine the constraint name.

if object_id('[dbo].[GetDefaultConstraintName]') is not null
    drop function [dbo].[GetDefaultConstraintName]
go

create function [dbo].[GetDefaultConstraintName] (
    @TableName varchar(max),
    @ColumnName varchar(max))
returns varchar(max)
as
begin

    -- Returns the name of the default constraint for a column

    declare @Command varchar(max)
    select 
        @Command = d.name
    from 
        ((
        sys.tables t join
        sys.default_constraints d
            on 
                d.parent_object_id = t.object_id) join
        sys.columns c
            on 
                c.object_id = t.object_id and 
                c.column_id = d.parent_column_id)
    where 
        t.name = @TableName and 
        c.name = @ColumnName
    return @Command
end
go

2. Generate dynamic sql to drop the constraint. Assume that the table name is MyTable and the column name is MyColumn.
execute ('alter table MyTable drop constraint '+
           [dbo].[GetDefaultConstraintName]('MyTable','MyColumn'))

3. Re-create the constraint with the desired default value. Assume that the new default value should be 33.
alter table MyTable add constraint [DF_MyTable_MyColumn] default (33) for MyColumn
1

View comments

I live in a relatively quiet town of Piscataway in New Jersey.  The township is mostly blue.  Which means that anyone running on a democratic ticket is most likely to win.

The mayor of this township is running for the 6th term.

I have been using Crystal Reports to create crappy looking reports in my VB (yes, many many years ago,) VB.Net, and C# apps.  I like it because I know it well and it is functional for the most part.  However, every new version of Crystal Reports brings in a lot of new headaches.

Sorry Mac folks, this article is very Windows specific, however, it is worth a read.  How many times it has happened to you that you had to look for a recipe you used or a code snippet you wrote several years ago.  That's easy, you say.

This is my first blog of 2016.  I think 2016 will be an exciting year for the world.

On a different note, the Powerball lottery's jackpot is a record $800 million.  My luck with any kind of gambling has been consistently very bad.  I do not win raffles, football pools etc.

1

Now that I was somewhat comfortable using Git for version control, I immediately felt the need to create some private repos. I needed a remote repository to backup my work. One option was to get a paid account with the good folks at the GitHub, or use my Linux hosting package at 1&1.

16

In 2011, I blogged about my experiences with the cheap hosting providers like GoDaddy and 1and1 Hosting.  In the blog I mentioned that 1and1 hosting's SQL panel allows you to run SQL DDL statements using dynamic SQL only.  All creates, drops, grants etc. must be issued using EXEC() statements.

As you know, this series is dedicated to those individuals whose work makes me feel insignificant, and my existence, meaningless.

I love Sudoku.  If I was to put these puzzles into easy, medium, hard, and expert categories, I can deal up to hard level, comfortably.

Back in February 2013, I started a series dedicated to outstanding individuals, whose accomplishments simply dwarfs our own.  In this blog I have chosen this honor to go to Jeremy Kun, a mathematics PhD student.

Around 1987, I was working downtown in New York City.  I started going to a local Irish pub to get my lunch (Not to be confused with beer.)  They served this wonderful dish called the corned beef.  I did not know how it was prepared but I just loved it.

1

With this post, I am starting a new segment called, "We are not worthy!"  The segment is essentially to honor ordinary people who create.

1

Moses said to Yahweh, "O Lord, I am not eloquent, neither before now, nor since you have spoken to your servant; for I am slow of speech, and of a slow tongue."

When it comes to discussing religion, I can relate to Moses.

If you work with sql server, I am sure you know the pains associated with the referential integrity.  Try deleting a top level row and you soon realize that a dozen related rows need to be deleted too.

1

At the time of writing this blog (Dec. 2011), the U.S. Postal Service is losing money, and it is losing it fast.  The projected figure of losses by September 30, 2011, was $10 billion.

2

Hurricane Irene is about to hit New Jersey in less than twenty four hours.  Local Home Depot and Lowes are already out of sump pumps, flash lights, batteries, and water.  In a nutshell, everyone is preparing for the "Storm of a Life Time." I am as ready as I would be in any rainy season.

4

The Problem: Can you create reusable composite web user controls?

Yes, you can!

If you are like me, you like to get most bang for your code. In the object oriented world you get this by creating reusable objects.

3

Sql server will not let you dropa table if it has default contraints (like default values for columns.)

This script will let you find them.

1. Determine the constraint name.

1

I do not know why people get so excited about barbecue in summer. What is so special about summer? I live in New Jersey, where summer really lasts 3-4 months. My point is, If you love barbecue you should be able to do it all year around. That is exactly what I do.

4

Not too long ago I managed mail servers where each message was stored in a file. Needless to say that there were literally hundreds of thousands of files on the file system.

6
About Me
About Me
My Photo
Piscataway, New Jersey, United States
I am an electrical engineer by education and a software developer by profession. I like building electro-mechanical models. I also like grilling and barbecuing with passion. To burn my beer cals, I swim and run. I usually post my DIY projects on: http://www.instructables.com/member/kabira/
Blog Archive
Loading
Dynamic Views theme. Powered by Blogger.