SQL server 停用全部外键和启用

用于批量删除和添加数据

1.停用全部外键
select
'ALTER TABLE '+o.name+' NOCHECK CONSTRAINT '+fk.name+';' AS Command
from
sys.foreign_keys fk
JOIN sys.all_objects o ON (fk.parent_object_id=o.object_id);

2.启用全部外键
select
'ALTER TABLE ' + o.name + ' CHECK CONSTRAINT ' + fk.name + ';' AS Command
from
sys.foreign_keys fk
JOIN sys.all_objects o ON (fk.parent_object_id = o.object_id)

你可能感兴趣的:(SQL server 停用全部外键和启用)