Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 copied the structure using

SELECT * INTO [BackupTable] FROM [OriginalTable]

But that doesn't copy indexes, constraints, triggers etc

Does anyone know how to do this?

like image 744
Byron Whitlock Avatar asked Mar 16 '09 21:03

Byron Whitlock


People also ask

How do I copy a table with indexes in SQL Server?

Select the DATABASE from which you want to copy the table, then right Click on that and Select the TASK then Select Generate Script. follow it. Step 3. Choose Script Options -> Table/View Options -> Select two indexes option and change both to True.

How do I select all indexes in SQL Server?

You can use the sp_helpindex to view all the indexes of one table. And for all the indexes, you can traverse sys. objects to get all the indexes for each table. Only problem with this is that it only includes the index key columns, not the included columns.

How do I copy data from one SQL Server table to another?

Launch SQL Server Management Studio. Select and right-click on the Source Database, go to Tasks > Export Data. Import/Export Wizard will be opened and click on Next to proceed. Enter the data source, server name and select the authentication method and the source database.


1 Answers

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

like image 108
TFD Avatar answered Oct 25 '22 17:10

TFD