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.  Most people in the township are not happy with this mayor's leadership, yet he keeps winning.  It's not like there is a shortage of well qualified folks for this post.  So, how does he do that?  The answer is simple.  Once he wins the democratic primary, he might as well open the champagne bottle in July.

This is where the rigging comes into play.  Note that nothing is illegal about it, but it should be.  This is how the democratic primary ballot for 2020 looks like.

Obviously, all the candidates are democrats.

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.  I have been maintaining an application which was kept as 32 bit app primarily because of Crystal Reports.  So, this weekend, I decided to try to convert the app to 64 bit anyway.

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.  You have a clever folder structure for organizing documents and you put your recipes and code snippets buried in those folders.  You have a crafty search program like Agent Ransack, which goes through your files in a second and find the content you were looking.

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.  I lose in casinos, and of course in the lottery.  I have accepted it as a positive thing, so I try to stay away from lottery or any gambling activity as much as possible.
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. I chose the later. I use GIT for Windows. By the way, Git already comes installed on the Linux servers on 1&1. I wanted to use a secured connection to the server.
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.  Now the question is, what makes a hard puzzle, hard.  You may think that it is the number of count of pre-solved numbers make it so.  Unfortunately, I have seen cases where a hard puzzle had more initial numbers than a medium one.

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.

What possible connection could be there between the movie, The Lord of the Rings and fast Fourier transform?  You can find the answer on Jeremy Kun's website.

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.  A few years later I was introduced to pastrami, which I think, is a close cousin of the corned beef, I loved it even better.

Over the years, I have gotten very good at making the corned beef from scratch.
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.  When I see the creativity of such people, it makes me humble and I simply want to say, "I am not worthy!"  Let me make something clear, I will never consider Arianna Huffington and Deepak Chopra for this honor, in spite of their accomplishments.  That is a topic for another day.
1
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.