Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

SQL
Select top 0 * INTO TAB2 from dbo.TAB1 


This will create new table with name as TAB2 having same schema as TAB1 table. Now problem is there is an index on TAB1 which isn't created on tab2. Is there any way to copy indexes as well.


I know we cannot create two indexes with same name, But is there any way to get fields covered in index and create it on TAB2 directly ???
Posted
Comments
RedDk 20-Nov-13 12:53pm    
Just make sure to include ORDER BY (whatever the index column is) ASC/DESC. If there's no index then create one first usg IDENTITY(1,1) in interrum table. Or use the original CREATE script for the table under question as the "clone".

It can be done but it's a vile hack, so brace yersels...

Set up a demo source table with three indexes and our empty copy table
SQL
create table fred
(
 a varchar(10),
 b varchar(10)
)
go
create unique index ix_fred_a on fred(a)
go
create index ix_fred_b on fred(b)
go
create index ix_fred_ab on fred(a,b)
go

select * into jim from fred where 1 = 0


We stuff the op from 'sp_helpindex fred' into a temp. table and use that to get the key names for each index.
SQL
create table #indexes
(
 index_name        varchar(255), 
 index_description varchar(255),
 index_keys        varchar(255)
)

insert into #indexes exec sp_helpIndex fred

-- Throw away the PK, that's another problem.
delete from #indexes where index_description like '%primary key%'


Now we use a cursor to work through all the indexes for fred building a bit of dynamic SQL to create the indexes on jim.

SQL
-- Name our destination.
declare @tableName varchar(50)
set     @tableName = 'jim'

declare @indexName varchar(50)
declare @description varchar(255)
declare @keys varchar(255)

declare getIndex cursor
for select index_description, index_keys from #indexes

open getIndex
fetch next from getIndex into @description, @keys
while @@fetch_status = 0
begin
  declare @unique varchar(20)
  declare @buildIx varchar(500)

  -- Create a name for our new index.
  set @indexName = rtrim('IX_' + replace(cast(newid() as varchar(40)),'-',''))

  -- Is the index unique or not?
  set @unique = ''
  if @description like '% unique %'
  begin 
    set @unique = ' unique '
  end

  -- Assemble and execute the index create.
  set @buildIx =  'create ' + @unique + 'index ' + @indexName +
                  ' on ' + @tableName + '(' + @keys +')'
  print @buildIX
  exec(@buildIX)

  fetch next from getIndex into @description, @keys
end
close getIndex
deallocate getIndex


If it works you should see something like the following...

SQL
create  unique index IX_6A8931B2364340FCB378D9EE768C56CC on jim(a)
create index IX_4A471647B31E4499AE9C13179C511EBC on jim(a, b)
create index IX_714ED1CF6FBF4CF388B4E904E8C2E665 on jim(b)


Finally just check that we've got what we hoped for.

SQL
sp_help jim
 
Share this answer
 
Hi,

I don't think there is as such option. What you can do you can check index on the TAB1 with query and create with different on TAB2.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900