Compare Page Revisions
« Older Revision - Back to Page History - Newer Revision »
/*--- This SQL generates a set of SQL statements to disable every foreign key in the database ---*/ select distinct GroupNum = 10 ,SqlCode = 'ALTER TABLE [' + onSchema.name + '].[' + onTable.name + '] NOCHECK CONSTRAINT [' + foreignKey.name + ']' from sysforeignkeys fk inner join sysobjects foreignKey on foreignKey.id = fk.constid inner join sys.objects onTable on fk.fkeyid = onTable.object_id inner join sys.schemas onSchema on onSchema.schema_id = onTable.schema_id inner join sysobjects againstTable on fk.rkeyid = againstTable.id where 1=1 AND againstTable.TYPE = 'U' AND onTable.TYPE = 'U' and ObjectProperty(fk.constid,'CnstIsDisabled') = 0 /*--- This SQL generates a set of SQL statements to delete data from every table ---*/ union select distinct GroupNum = 20 ,SqlCode = 'delete from [' + s.name + '].[' + o.name + ']' FROM sys.objects o inner join sys.schemas s on o.schema_id = s.schema_id where 1=1 and o.type = 'U' and o.name not in ('__MigrationHistory', '__EFMigrationsHistory') /*--- This SQL generates a set of SQL statements to re-enable every foreign key in the database which was originally enabled ---*/ union select distinct GroupNum = 30 ,SqlCode = 'ALTER TABLE [' + onSchema.name + '].[' + onTable.name + '] CHECK CONSTRAINT [' + foreignKey.name + ']' from sysforeignkeys fk inner join sysobjects foreignKey on foreignKey.id = fk.constid inner join sys.objects onTable on fk.fkeyid = onTable.object_id inner join sys.schemas onSchema on onSchema.schema_id = onTable.schema_id inner join sysobjects againstTable on fk.rkeyid = againstTable.id where 1=1 AND againstTable.TYPE = 'U' AND onTable.TYPE = 'U' and ObjectProperty(fk.constid,'CnstIsDisabled') = 0 order by GroupNum
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.