Reputation: 195
I have to write a script where in I need to drop a foreign key constraint, the problem is that I do not know the constraint name.
I know I could use sp_help CHILD_TABLE_NAME
to find the name of the constraint and then run ALTER TABLE CHILD_TABLE_NAME DROP CONSTRAINT CONSTRAINT_NAME
to drop the foreign key constraint, But since I'm writing a script I need to do that programatically through SQL code.
Upvotes: 1
Views: 904
Reputation: 1362
The following query will give you a list of all the foreign key names on a given table. You should be able to iterate through them from there.
(Hint unless you want to drop them permanently it's better to disable them).
SELECT name AS FK_Name
FROM sys.foreign_keys
WHERE parent_object_id = OBJECT_ID('MyObjectName')
Upvotes: 1