SQL Server 2012 – Prevent User Access During Index Rebuild

sql-server-2012

I need to rebuild the indexes in two very large (almost 750 table) database but want to prevent users from accessing the database while this happens. Is there a state I can put the database in that prevents the users from accessing it but still allows me to rebuild the indexes? For reference I plan on the script provided at http://instadba.com/quick-script-to-defragment-your-sql-server-indexes/ to do the rebuild.

Best Answer

USE master;
GO
ALTER DATABASE YourDatabase 
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
USE YourDatabase;
--Run index maint scripts...
GO
ALTER DATABASE YourDatabase
SET MULTI_USER;
GO