Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Colonna di rilascio di SQL Server 2005 con vincoli

Ecco uno script che eliminerà la colonna insieme al suo vincolo predefinito. Sostituisci MYTABLENAME e MYCOLUMNNAME in modo appropriato.

declare @constraint_name sysname, @sql nvarchar(max)

select @constraint_name = name 
from sys.default_constraints 
where parent_object_id = object_id('MYTABLENAME')
AND type = 'D'
AND parent_column_id = (
    select column_id 
    from sys.columns 
    where object_id = object_id('MYTABLENAME')
    and name = 'MYCOLUMNNAME'
    )

set @sql = N'alter table MYTABLENAME drop constraint ' + @constraint_name
exec sp_executesql @sql

alter table MYTABLENAME drop column MYCOLUMNNAME

go