How to copy indexes from one table to another in SQL Server

后端 未结 6 2218
北海茫月
北海茫月 2020-12-15 05:38

I need to copy the indexes from one table to another. There are a LOT of indexes and I don\'t want to recreate them from scratch. Seems error prone anyways.

I have c

相关标签:
6条回答
  • 2020-12-15 06:19

    I'm not specifically familiar with SQL Server, however based on the way database tables are defined and used in other databases, i would say there is no way to do this by just copying the data or using a SELECT INTO.

    The indexes need to be defined as part of the table structure and need to be generated for any existing data that may be in the table.

    The only way to do this is during the CREATE TABLE statement of by using an ALTER TABLE statement after the table has been created.

    Statements for working with the data itself are separate to working with the table definitions so i don't think there will be any work around or shortcut for this.

    I'm sure there would be tools available to generate the ALTER TABLE statement to create all the appropriate indexes based on the table you already have, making it easy and reliable.

    0 讨论(0)
  • 2020-12-15 06:19

    You could build a script using the information in sysindexes (or sys.indexes depending on your version) to recreate all of the indexes, but using the Select * into approach is also not going to pick up up foreign and primary keys or any extended proprties. You should really look into using SSIS if you are using a 2005+ version, or DTS if you are using 2000, both have wizards to simplify this kind of copy.

    SSIS Import Export

    DTS Import Export

    0 讨论(0)
  • 2020-12-15 06:22

    Rightclick the index in SSMS and do script as > create .

    Change the table name and the index name and you should be set

    0 讨论(0)
  • 2020-12-15 06:23

    You should get the idea here

    Sub CopyTemp(tblName As String, Optional WithIndex As Boolean = False)
        Dim db As DAO.Database
        Dim tDefOrig As DAO.TableDef
        Dim tDefNew As DAO.TableDef
    
        Dim idxOrig As DAO.Index
        Dim idxNew As DAO.Index
        Dim fld As DAO.Field
    
        Dim tempName As String
    
        tempName = "temp" & tblName
    
        Set db = CurrentDb
        db.Execute "Select * Into " & tempName & " From " & tblName
    
        If WithIndex = True Then
            Set tDefOrig = db.TableDefs(tblName)
            Set tDefNew = db.TableDefs(tempName)
    
            For Each idxOrig In tDefOrig.Indexes
                Set idxNew = tDefNew.CreateIndex(idxOrig.Name)
                With idxNew
                    For Each fld In idxOrig.Fields
                        .Fields.Append .CreateField(fld.Name)
                    Next
                    .Primary = idxOrig.Primary
                    .Unique = idxOrig.Unique
                End With
                tDefNew.Indexes.Append idxNew
                tDefNew.Indexes.Refresh
            Next
        End If
    
    End Sub
    
    0 讨论(0)
  • 2020-12-15 06:24

    Do you want to copy the Index definition?

    Then you can reverse engineer the index, triggers etc using the "Script" option in the Microsoft SQL Management tool

    Simply right click on a table name in the SQL Management Studio table list and select "Script Table as" and then "Create to"

    You can't copy the Index data as it relates to the physical storage of the Index

    First check that you have "Tools/Options/SQL Server Object Explorer/Scripting/Script Indexes" set to "True". This is set to false in some version of the SQL Management tool (thanks Mark)

    enter image description here

    0 讨论(0)
  • 2020-12-15 06:36

    By default the right-click table "CREATE" does not include the indexes or triggers, just the table definition and constraints.

    You can right click the database and click "Tasks" -> "Generate Scripts" which will allow you to do this

    Edit: this is the default but as TFD mentions it can be changed, thankfully.

    0 讨论(0)
提交回复
热议问题