2012-10-01 13 views
5

Qualcuno ha uno script per elencare le istruzioni CREATE INDEX per tutti gli indici esistenti in un database SQL Server?Genera istruzioni CREATE INDEX in SQL Server

Questa discussione List of all index & index columns in SQL Server DB offre ottimi consigli su come trovarli. Ma uno script per generare le dichiarazioni CREATE INDEX sarebbe fantastico. A volte entriamo in una situazione senza dati adeguati, o gli indici sono stati aggiunti in un modo ad hoc nel tempo senza documentazione, quindi mancano le dichiarazioni di creazione. Come in una situazione, mi trovo in questo momento.

Grazie.

+1

Possibile duplicato di [Genera script di Tutti gli indici in un database] (http://stackoverflow.com/questions/9089700/generate-script-of-all-the-indexes-in-a-database) – bummi

risposta

10

Usa generare script da SQL Management Studio e scegliere le opzioni di "indici Script" (in Opzioni avanzate di scripting)

+0

Questo ha funzionato perfettamente Grazie! (E grazie anche agli altri, apprezzo il tuo aiuto). – royappa

+3

Potrei affermare l'ovvio, ma per arrivarci, fai clic con il tasto destro del mouse sul database -> Attività -> Genera script ... –

3

ho scritto qualcosa per che qualche tempo fa. Potrebbe essere necessario modificarlo per le tue esigenze, ma almeno hai uno scheletro.

if exists (select 1 from information_schema.routines where routine_name = 'Script_CreateIndex') 
    drop proc Script_CreateIndex 
go 

create proc Script_CreateIndex (
    @TableName varchar(124) 
) 
as 
begin 
    if not exists (select 1 from sys.indexes where object_name(object_id) = @TableName and type_desc in ('CLUSTERED', 'NONCLUSTERED')) 
     return 

    declare @IndexList table (
     Id int identity, 
     IndexName varchar(124), 
     IndexDescription varchar(max), 
     IndexKeys varchar(max) 
    ) 

    insert @IndexList(IndexName, IndexDescription, IndexKeys) 
     exec sp_helpindex @TableName 

    if (select count(*) from @IndexList) > 0 
    begin 
     select '-- Creating indexes for table ' + @TableName 

     while exists (select 1 from @IndexList) 
     begin 
      declare @Id int, @IndexName varchar(124), @IndexDescription varchar(max), @IndexKeys varchar(max) 
      select top 1 @Id = Id, @IndexName = IndexName, @IndexDescription = IndexDescription, @IndexKeys = IndexKeys from @IndexList order by Id 
      delete from @IndexList where Id = @Id 

      declare @Clustered varchar(10), @Unique varchar(7) 

      select @Clustered = case when patindex('%nonclustered%', @IndexDescription) > 0 then '' else ' clustered ' end 
      select @Unique = case when patindex('%unique%', @IndexDescription) > 0 then ' unique ' else '' end 

      select 'if not exists (select 1 from sys.indexes where name = ''' + @IndexName + ''')' 
      select 'begin' 
      select char(9) + 'create' + @Unique + @Clustered + ' index [' + @IndexName + '] on [' + @TableName + '](' + @IndexKeys + ')' 
      select char(9) + 'select ''Index ' + @IndexName + ' created.''' 
      select 'end' 
      select 'go' 
     end 

     select '' 
     select '' 
    end 
end 
go 

grant exec on Script_CreateIndex to public 
select 'Script_CreateIndex compiled.' 'Job' 
go 
1

Puoi farlo su un tavolo da basi tabella utilizzando la finestra "Object Explorer"

Vai agli indici cartella in studio Management, evidenziare la cartella quindi aprire il riquadro Esplora oggetti

Puoi quindi "spostare Seleziona" tutti gli indici su quella tabella, se fai clic con il tasto destro del mouse sullo script "CREA A" creerà uno script con tutti gli indici rilevanti per te.

Problemi correlati