#foreach ($fk in $table.ForeignKeys) IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='$fk.Name') ALTER TABLE $table.Name DROP CONSTRAINT $fk.Name; #end IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = '$table.Name') BEGIN DECLARE @reftable nvarchar(60), @constraintname nvarchar(60) DECLARE refcursor CURSOR FOR select reftables.name tablename, cons.name constraitname from sysobjects tables, sysobjects reftables, sysobjects cons, sysreferences ref where tables.id = ref.rkeyid and cons.id = ref.constid and reftables.id = ref.fkeyid and tables.name = '$table.Name' OPEN refcursor FETCH NEXT from refcursor into @reftable, @constraintname while @@FETCH_STATUS = 0 BEGIN exec ('alter table '+@reftable+' drop constraint '+@constraintname) FETCH NEXT from refcursor into @reftable, @constraintname END CLOSE refcursor DEALLOCATE refcursor DROP TABLE $table.Name END