Technique to change contstaint name in case constraint name is auto generate or not know.

Question:

Declare @Table_Object_iD int
Declare @Column_Object_iD int
Declare @Cont_Name varchar(100)

SELECT @Table_Object_iD=object_id FROM sys.tables where name='B_Project_AnnexureA2'
SELECT @Column_Object_iD=column_id FROM sys.columns where object_id=@Table_Object_iD and name='LiftsPassengerCapacity'
SELECT @Cont_Name=name FROM sys.default_constraints where parent_object_id=@Table_Object_iD and parent_column_id=@Column_Object_iD

EXEC ('ALTER TABLE [B_Project_AnnexureA2] DROP CONSTRAINT ['+ @Cont_Name +']')

ALTER TABLE B_Project_AnnexureA2 ALTER COLUMN LiftsPassengerCapacity int

EXEC ('ALTER TABLE [B_Project_AnnexureA2] ADD CONSTRAINT [DF_B_Proj_AnnA2_LfPassCpty] DEFAULT ((0)) FOR [LiftsPassengerCapacity]')
Creation date: 7/8/2019 5:55 PM     Updated: 7/8/2019 5:55 PM