The answer as follows is wrong:
-- Desabilita constraints de todas as tabelas
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
-- Reabilita constraints de todas as tabelas
EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'
In accordance with: Stackoverflow (in English) and Sqlauthority (in
english)
But attention, your tables will be unmanned until rehabilitation
of the same.
In production environments, use sparingly.
Primary keys cannot be disabled, need to be droppings/removed. 3
Already considering the added requirement in the commentary, about keeping state.
Algorithmically speaking, your exit would be something like: `
Salva estado atual -> Dropa todas as constraints -> Retorna o estado atual
Perhaps the best solution is export their constraints/retributions, with the Generate Scripts Wizard , or script, and then drop them, importing again completed the task.
Later I will think of something programmatic, nothing safe for a long-standing realistic production environment, but perhaps for normalized relational models and on certain considerations such as simple keys.
There is no risk of enabling a
CONSTRAINT
that was already disabled for some other reason when you enable all later?– Sorack
Ah, for sure!
– sbrubes
Can you formulate a safer answer then?
– Sorack