While adopting some new computed fields in our database, we need to be able to revert the changes and then put them back again.
The essential problem can be explained with the following script. It does not work, because there is a generated constraint that should be dropped before dropping the column Field1
:
CREATE TABLE Table1
(
Field1 DECIMAL(19, 2) NOT NULL DEFAULT(0.0),
Field2 AS Field1
)
GO
ALTER TABLE Table1
DROP COLUMN Field2, Field1
GO
DROP TABLE Table1
GO
SSMS shows:
Msg 5074, Level 16, State 1, Line 10
The object 'DF__Table1__Field1__5AC46587' is dependent on column 'Field1'.Msg 4922, Level 16, State 9, Line 10
ALTER TABLE DROP COLUMN Field1 failed because one or more objects access this column.
The code works, if the constraint DF__Table1__Field1__5AC46587
is dropped before dropping the column Field1
:
ALTER TABLE Table1
DROP CONSTRAINT DF__Table1__Field1__5AC46587
GO
The problem is that the name of the constraint is different each time when Field2
is created.
Is it possible to have a constant name for this constraint or somehow find it easily in the script?