Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Generate Index script
Message
De
02/01/2012 05:05:14
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
 
À
01/01/2012 17:23:36
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Scripting
Versions des environnements
SQL Server:
SQL Server 2008
Divers
Thread ID:
01531981
Message ID:
01532023
Vues:
48
>>>You may as well roll your own
>
>Thanks for the tips
>I found some like this one the web, following similar methods.
>I'm still hoping to find a fully-fleshed script.

You may look at Toad (http://toadforsqlserver.com), it's the best replacement for enterprise manager and query analyzer (and for other tools, if you pay). It also generates a lot of stuff, including indexes. You get scripts like this:
CREATE NONCLUSTERED INDEX [xxx_PK]
   ON [dbo].[xxxtable] ([xxx_pk])
   WITH (PAD_INDEX = OFF,
         FILLFACTOR = 100,
         IGNORE_DUP_KEY = OFF,
         STATISTICS_NORECOMPUTE = OFF,
         ONLINE = OFF,
         ALLOW_ROW_LOCKS = ON,
         ALLOW_PAGE_LOCKS = ON,
         DATA_COMPRESSION = NONE)
   ON [PRIMARY];
GO
But in the freebie version you have to visit one table at a time, copy the index part of the script, paste it into your big script and that's a lot of work and you're never sure if you forgot something. Furthermore, you'll get a lot of errors for indexes which already exist - you still need some more scripting to find which indexes exist, or a table to carry with your tool and to check against (that table may as well contain the script for each index, so you'd run by sending the memo with the script straight to the server).

Most of the tools out there assume your target db is of a known version, and generate scripts to bridge the difference between it and the current version. Of course, in real life, your target db is unknown number of versions behind, which will make that generated script fail miserably, or (worse) do its job only to leave you to discover that the db is now missing the last three upgrades, but has the features from the most current one. One customer even demanded to see each script before I apply it to the database... something even I never see, because the tool I wrote generates them on the fly, comparing the features (current tables, fields, constraints, indexes, triggers, SPs) listed in the metadata with those on the target database, and bringing it to the desired state.

Ah, just found the "check for existence" checkbox in Toad... so now it generates the above script with this part around the CREATE ... INDEX:
IF NOT EXISTS
          (SELECT *
             FROM sys.indexes
            WHERE     object_id = OBJECT_ID (N'[dbo].[xxxtable]')
                  AND name = N'xxx_pk')
   BEGIN
...
   END
You'd still have to peek into this for each table and copy-paste into your big script. Then do that each time. You only get automated creation of pieces you need to assemble manually.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform