Does anyone know if there is a command where you can first check to see if a primary key exists or not before you add or drop it......similar to DROP TABLE IF EXISTS .....?
I currently use:
ALTER TABLE employees DROP CONSTRAINT [PK_Employees]
to drop the PK and
ALTER TABLE employees ADD CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
[Empl_Id] ASC,
[Row Id] ASC
)
to add the primary key back after some other processing is done.
This works fine, but you can see if things get out of order and it tries to drop the constraint when it doesn't exist in the first place, I will get an error.
I currently use:
ALTER TABLE employees DROP CONSTRAINT [PK_Employees]
to drop the PK and
ALTER TABLE employees ADD CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
[Empl_Id] ASC,
[Row Id] ASC
)
to add the primary key back after some other processing is done.
This works fine, but you can see if things get out of order and it tries to drop the constraint when it doesn't exist in the first place, I will get an error.