我正在寻找一种方法来暂时关闭所有数据库的约束(例如表关系)。
我需要将一个数据库的表复制(使用 INSERT)到另一个数据库。 我知道我可以通过按正确的顺序执行命令来实现这一点(不破坏关系)。
但是如果我可以暂时关闭检查约束并在操作完成后重新打开它会更容易。
这可能吗?
最佳答案
-- Disable the constraints on a table called tableName:
ALTER TABLE tableName NOCHECK CONSTRAINT ALL
-- Re-enable the constraints on a table called tableName:
ALTER TABLE tableName WITH CHECK CHECK CONSTRAINT ALL
---------------------------------------------------------
-- Disable constraints for all tables in the database:
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
-- Re-enable constraints for all tables in the database:
EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL'
---------------------------------------------------------
关于sql - 暂时关闭约束 (MS SQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/737115/