I'm trying to drop a few constraints which have been automatically generated when I add the default value somewhere.
I use the following script to return me the names of the constraints:
SELECT default_constraints.name FROM sys.all_columns INNER JOIN sys.tables ON all_columns.object_id = tables.object_id INNER JOIN sys.schemas ON tables.schema_id = schemas.schema_id INNER JOIN sys.default_constraints ON all_columns.default_object_id = default_constraints.object_id WHERE tables.name = 'TrainingType' AND default_constraints.name like 'DF__TrainingT__Soft__%' OR default_constraints.name like 'DF__TrainingT__EndUs__%' OR default_constraints.name like 'DF__TrainingC__Compu__%' This returns me the following:
| name --------------------------------- 1 | DF__TrainingC__Compu__2058C9F1 2 | DF__TrainingT__EndUs__1559B68C 3 | DF__TrainingT__Softw__05CD5A39 Now I'm trying to drop the constraints with these values, but it doesn't allow me to do DROP CONSTRAINT ( ... )
ALTER TABLE TrainingType DROP CONSTRAINT ( SELECT default_constraints.name FROM sys.all_columns INNER JOIN sys.tables ON all_columns.object_id = tables.object_id INNER JOIN sys.schemas ON tables.schema_id = schemas.schema_id INNER JOIN sys.default_constraints ON all_columns.default_object_id = default_constraints.object_id WHERE tables.name = 'TrainingType' AND default_constraints.name like 'DF__TrainingT__Soft__%' OR default_constraints.name like 'DF__TrainingT__EndUs__%' OR default_constraints.name like 'DF__TrainingC__Compu__%' ) So how can I drop the constraints correctly?
EXEC?