Skip to main content
Inspiring
June 23, 2008
Answered

Dropping a constraint in MS SQL Server

  • June 23, 2008
  • 1 reply
  • 919 views
Hi All,

Does anybody know if there's a way to check if a constraint exists before dropping it?

In other words, how do I check if constraint "FK_Products1_ID" exists before dropping it (see below) so I won't get the error " 'FK_Products1_ID' is not a constraint. Could not drop constraint"

ALTER TABLE Products2_Table DROP CONSTRAINT FK_Products1_ID

Thanks in advance!



This topic has been closed for replies.
Correct answer AppDeveloper-Sp9Jq0
Thank you very much, Ken. It works.

The correct syntax:

IF EXISTS (select * from dbo.sysobjects where name = 'FK_Products1_ID')
BEGIN
ALTER TABLE Products2_Table DROP CONSTRAINT FK_Products1_ID
END

Sorry about not mentioning what version of sql server.
Yes, I'm using version 2k.

1 reply

Inspiring
June 23, 2008
You don't say what rdms and what version you are using.

So, here's an example for sql server 2k

if exists (select name from sys.objects where name = 'FK_Products1_ID')
ALTER TABLE Products2_Table DROP CONSTRAINT FK_Products1_ID

Ken
AppDeveloper-Sp9Jq0AuthorCorrect answer
Inspiring
June 23, 2008
Thank you very much, Ken. It works.

The correct syntax:

IF EXISTS (select * from dbo.sysobjects where name = 'FK_Products1_ID')
BEGIN
ALTER TABLE Products2_Table DROP CONSTRAINT FK_Products1_ID
END

Sorry about not mentioning what version of sql server.
Yes, I'm using version 2k.