Turning off consistency check on Microsoft SQL script restore
-
13-11-2019 - |
Question
On several other databases I've come across, one can turn off the consistency check when restoring a (scripted) dump of the database. The idea is that when you have dumped the script from a database, you can restore it safely, without doing consistency checks, both to speed things up, and to not be hindered by the order which you stash in the different tables, foreign keys, views etc.
(On Microsoft SQL (2005) you have the 'generate and publish scripts'.)
Is there such a switch for MS SQL? And obviously, how do you turn it on again - and how can you potentially force a global consistency check afterwards?
PS: I do not necessarily need this feature to execute scripts which the server has produced..!
Solution
I dont think it can be donne, but Im not sure.
What can be donne is manually disable the constraint with the code:
ALTER TABLE table_name
NOCHECK CONSTRAINT constraint_name
and enable it using the same syntax, just change NOCHECK to CHECK
If you really need it to the whole DB you can easly build something to loop through the tables and constraints and execute this code
OTHER TIPS
The "generate scripts" option (2005) for a given database produces the code required to create the database objects only. It does not script out the table contents (as per MySQL for example) and hence no need for an option to disable constraint checking.
If you wish to restore a copy of a database and it's data then the preferable route is to perform a Database Backup, followed by a database Restore.
There are some third party backup products that will enable you to restore specific objects only, from a Full database backup.