Drop default constraint on a column in TSQL

前端 未结 3 623
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 12:01

I have a table with a column like this that is currently live:

name NVARCHAR(128) NOT NULL DEFAULT \'\'
3条回答
  •  天涯浪人
    2020-12-28 13:01

    This is how you would drop the constraint

    ALTER TABLE .
       DROP CONSTRAINT 
    GO
    

    With a script

    -- t-sql scriptlet to drop all constraints on a table
    DECLARE @database nvarchar(50)
    DECLARE @table nvarchar(50)
    
    set @database = 'dotnetnuke'
    set @table = 'tabs'
    
    DECLARE @sql nvarchar(255)
    WHILE EXISTS(select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where constraint_catalog = @database and table_name = @table)
    BEGIN
        select    @sql = 'ALTER TABLE ' + @table + ' DROP CONSTRAINT ' + CONSTRAINT_NAME 
        from    INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
        where    constraint_catalog = @database and 
                table_name = @table
        exec    sp_executesql @sql
    END
    

    Credits go to Jon Galloway http://weblogs.asp.net/jgalloway/archive/2006/04/12/442616.aspx

提交回复
热议问题