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.  However, column B sticks out more because of stalwart names like Vice President Biden, Senator Booker, and Congressman Pallone.  This visual association gives Mayor Wahler an undue advantage over his opponents.  Take a look at the visuals below and tell me which path looks less complicated to a human brain.




0

Add a comment

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.