dastaub
asked on
SQL 2005
How I can check if the field 'Manager' already exists before I attempt to add the field?
ALTER TABLE dbo.Facility ADD
Manager varbinary(50) NULL
GO
How can I check if the constraint of a default value of 1 for the field CliNo already exists before I add the constraint.
ALTER TABLE dbo.Facility ADD CONSTRAINT
DF_Facility_CliNo DEFAULT 1 FOR CliNo
GO
ALTER TABLE dbo.Facility ADD
Manager varbinary(50) NULL
GO
How can I check if the constraint of a default value of 1 for the field CliNo already exists before I add the constraint.
ALTER TABLE dbo.Facility ADD CONSTRAINT
DF_Facility_CliNo DEFAULT 1 FOR CliNo
GO
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i added the drop to give the script the ability to edit the constraint or column rather than only add if not present.
ASKER
BEGIN
ALTER TABLE dbo.Facility DROP CONSTRAINT DF_Facility_CliNo
END
GO
ALTER TABLE dbo.Facility ADD CONSTRAINT DF_Facility_CliNo DEFAULT 1 FOR CliNo
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
BEGIN
ALTER TABLE dbo.Facility DROP COLUMN Manager
END
GO
ALTER TABLE dbo.Facility ADD Manager varbinary(50) NULL