Search This Blog

Friday, May 14, 2010

Generate Script to drop all foreign key constraints in a database

From http://www.sqlservercentral.com/scripts/Replication/31530/




SET NOCOUNT ON

print 'USE ' + DB_NAME()
print ''

-- Generate Drops for All Foreign Keys in Database
print '-- Drop Foreign Keys'
print ''

select distinct 'ALTER TABLE [dbo].[' + object_name(fkeyid) +
'] DROP CONSTRAINT ' + object_name(constid) +
CHAR(13) + CHAR(10) + 'go'
from sysforeignkeys
go