SQL Query to drop all FK constraints

This query generates statements to drop all foreign key constraints (provided that you named them ‘FK_‘.
It also shows the statements that fails upon executing (because still referenced by other), so you can run them again, until finished.
I thought it might be handy.

It’s based on a script by … i found at ….

SELECT  
'begin try ALTER TABLE ' + so.table_name + ' DROP CONSTRAINT ' + so.constraint_name + ' end try '  +
'begin catch print ''ALTER TABLE ' + so.table_name + ' DROP CONSTRAINT ' + so.constraint_name + ''' end catch'
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS so   
WHERE so.constraint_name LIKE 'FK_%'

comments

No comments yet.

Sorry, the comment form is closed at this time.