sqlserver暂时停用全部外键

用于批量删除和添加数据

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)



作者:天才知道_2a25
链接:https://www.jianshu.com/p/011735b8d863
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2019-11-26 11:04  张开开  阅读(520)  评论(0编辑  收藏  举报