The object 'DF__*' is dependent on column '*' - Changing int to double

前端 未结 9 1446
面向向阳花
面向向阳花 2020-11-30 19:07

Basically I got a table in my EF database with the following properties:

public int Id { get; set; }
public string Title { get; set; }
public string Descript         


        
9条回答
  •  青春惊慌失措
    2020-11-30 19:21

    While dropping the columns from multiple tables, I faced following default constraints error. Similar issue appears if you need to change the datatype of column.

    The object 'DF_TableName_ColumnName' is dependent on column 'ColumnName'.

    To resolve this, I have to drop all those constraints first, by using following query

    DECLARE @sql NVARCHAR(max)=''     
    SELECT @SQL += 'Alter table ' + Quotename(tbl.name) + '  DROP constraint ' + Quotename(cons.name) + ';'
     FROM SYS.DEFAULT_CONSTRAINTS cons 
     JOIN   SYS.COLUMNS col ON col.default_object_id = cons.object_id
     JOIN   SYS.TABLES tbl ON tbl.object_id = col.object_id
     WHERE  col.[name] IN ('Column1','Column2')
    
    --PRINT @sql
    EXEC Sp_executesql @sql 
    

    After that, I dropped all those columns (my requirement, not mentioned in this question)

    DECLARE @sql NVARCHAR(max)='' 
    SELECT @SQL += 'Alter table ' + Quotename(table_catalog)+ '.' + Quotename(table_schema) + '.'+ Quotename(TABLE_NAME) 
                   + '  DROP column ' + Quotename(column_name) + ';'
    FROM   information_schema.columns where COLUMN_NAME IN ('Column1','Column2')  
    --PRINT @sql
    EXEC Sp_executesql @sql 
    

    I posted here in case someone find the same issue.

    Happy Coding!

提交回复
热议问题