How do I drop a foreign key constraint only if it exists in sql server? -


I can leave a table if it exists using the following code, but how to do it with the hindrance Is:

  if present (choose from sys.objects 1) WHERE OBJECT_ID = OBJECT_ID (N'TableName ') and type = (N'U')) drop table table name  

I also add the barrier using this code:

  Alternate table [dbo]. [Table name] Add the check with the check [FK_TableName_TableName2] Foreign Key ([FK_Name]) reference [dbo]. [Table name 2] ([ID])   

  if existing (SELECT * From the sys.foreign_keys WHERE object_id = OBJECT_ID (N'dbo.FK_TableName_TableName2 ') and parent_object_id = OBJECT_ID (N'dbo.TableName')) optional table [dbo.TableName] drop content [FK_TableName_TableName2]  
< / Div>

Comments