Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DROP CONSTRAINT Syntax

Status
Not open for further replies.

tmunson99

MIS
Jun 18, 2004
62
US
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.
 
you can probably do a query on sysobject/syscolumns and sysconstraints system tables todo the check

download a copy of the make up of these tables and how they join:


[bandito] [blue]DBomrrsm[/blue] [bandito]

[blue]Software code, like laws and sausages, should never be examined in production[/blue][black] - [/black][purple]Edward Tenner[/purple]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top