Drop default constraint on a column in TSQL

前端 未结 3 624
没有蜡笔的小新
没有蜡笔的小新 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 12:54

    I would suggest:

    DECLARE @sqlStatement nvarchar(MAX),
            @tableName nvarchar(50) = 'TripEvent',
            @columnName nvarchar(50) = 'CreatedDate';
    
    SELECT                  @sqlStatement = 'ALTER TABLE ' + @tableName + ' DROP CONSTRAINT ' + dc.name + ';'
            FROM            sys.default_constraints AS dc
                LEFT JOIN   sys.columns AS sc
                    ON      (dc.parent_column_id = sc.column_id)
            WHERE           dc.parent_object_id = OBJECT_ID(@tableName)
            AND         type_desc = 'DEFAULT_CONSTRAINT'
            AND         sc.name = @columnName
    PRINT'   ['+@tableName+']:'+@@SERVERNAME+'.'+DB_NAME()+'@'+CONVERT(VarChar, GETDATE(), 127)+';  '+@sqlStatement;
    IF(LEN(@sqlStatement)>0)EXEC sp_executesql @sqlStatement
    
    0 讨论(0)
  • 2020-12-28 13:01

    This is how you would drop the constraint

    ALTER TABLE <schema_name, sysname, dbo>.<table_name, sysname, table_name>
       DROP CONSTRAINT <default_constraint_name, sysname, default_constraint_name>
    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

    0 讨论(0)
  • 2020-12-28 13:02

    I would like to refer a previous question, Because I have faced same problem and solved by this solution. First of all a constraint is always built with a Hash value in it's name. So problem is this HASH is varies in different Machine or Database. For example DF__Companies__IsGlo__6AB17FE4 here 6AB17FE4 is the hash value(8 bit). So I am referring a single script which will be fruitful to all

    DECLARE @Command NVARCHAR(MAX)
         declare @table_name nvarchar(256)
         declare @col_name nvarchar(256)
         set @table_name = N'ProcedureAlerts'
         set @col_name = N'EmailSent'
    
         select @Command ='Alter Table dbo.ProcedureAlerts Drop Constraint [' + ( select 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 = @table_name
             and c.name = @col_name) + ']'
    
        --print @Command
        exec sp_executesql @Command
    

    It will drop your default constraint. However if you want to create it again you can simply try this

    ALTER TABLE [dbo].[ProcedureAlerts] ADD DEFAULT((0)) FOR [EmailSent]
    

    Finally, just simply run a DROP command to drop the column.

    0 讨论(0)
提交回复
热议问题