views:

49

answers:

3

I'm using SQL Server 2008.

Is it possible to create a script to loop over all tables in a database generating a set of index drop scripts and create scripts separately?

What I have to do is drop all indexes on a set of databases to run a heavy data load process but then I want to re-enable all the indexes. I don't want to have to go through each table and script an index drop and then index create.

A: 

Yes. Someone's already done it so no need to write a script again... :-)

Drop All Indexes and Stats in one Script

Edit. Oops. To create...

Before you drop them... In SSMS, right-click, generate scripts, etc

gbn
So how's he going to re-create them :)
dcp
I ended up using this script to do the drops then I found an internal tool someone created to script out all the indexes thanks :).
Fouad Masoud
+1  A: 

I think what you want to do is here. You don't really need to drop the indexes, just disable them while your load is occurring. This script will allow you to disable the indexes for your load, then re-enable when done, and the beauty is it does it all in one shot for the whole database with a single command.

dcp
That's an outstanding SP.
rwmnau
The problem with disabling the index is you can't access the table anymore, if its clustered I think.
Fouad Masoud
A: 

You could do it with SMO, take a look at SMO Script Index and FK's on a database

SQLMenace