SQL Server 2005 drop column with constraints

后端 未结 13 1004
一向
一向 2020-12-07 23:54

I have a column with a \"DEFAULT\" constraint. I\'d like to create a script that drops that column.

The problem is that it returns this error:

Msg 50         


        
13条回答
  •  情歌与酒
    2020-12-08 00:46

    This query finds default constraints for a given table. It aint pretty, I agree:

    select 
        col.name, 
        col.column_id, 
        col.default_object_id, 
        OBJECTPROPERTY(col.default_object_id, N'IsDefaultCnst') as is_defcnst, 
        dobj.name as def_name
    from sys.columns col 
        left outer join sys.objects dobj 
            on dobj.object_id = col.default_object_id and dobj.type = 'D' 
    where col.object_id = object_id(N'dbo.test') 
    and dobj.name is not null
    

    [EDIT] Updated per Julien N's comment

提交回复
热议问题