使用SQL语句创建表的克隆,其中克隆将包含所有索引 [英] Create Clone of Table using SQL statement where Clone will have all indexes

查看:79
本文介绍了使用SQL语句创建表的克隆,其中克隆将包含所有索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



 选择  top   0  *  INTO  TAB2 来自 dbo.TAB1 





这将创建名为TAB2的新表,其具有与TAB1表相同的模式。现在的问题是TAB1上有一个索引,它不是在tab2上创建的。有没有办法复制索引。





我知道我们不能创建两个同名的索引,但有什么办法吗?获取索引覆盖的字段并直接在TAB2上创建它???

解决方案

它可以完成,但它是一个卑鄙的黑客,所以支持yersels ...



设置一个包含三个索引和空副本表的演示源表

  create   table  fred 

a varchar 10 ),
b varchar 10

go
create 唯一 索引 ix_fred_a on fred(a)
go
创建 index ix_fred_b on fred(b)
go
创建 index ix_fred_ab on fred(a,b)
go

选择 * 进入 jim 来自 fred 其中 1 = 0





我们把sp_helpindex fred中的op填入temp。 table并使用它来获取每个索引的键名。

  create   #indexes 

index_name varchar 255 ),
index_description varchar 255 ),
index_keys varchar 255


插入 进入 #indexes exec sp_helpIndex fred

< span class =code-comment> - 扔掉PK,这是另一个问题。
删除 来自 #indexes 其中​​ index_description 喜欢 ' %主键%'





现在我们使用游标通过fred的所有索引来构建一些动态SQL来在jim上创建索引。



   -   命名我们的目的地。 
声明 @ tableName varchar 50
set @ tableName = ' jim'

声明 @ indexName varchar 50
声明 @ description varchar 255 )
声明 @ keys varchar 255

声明 getIndex cursor
选择 index_description,index_keys 来自 #indexes

open getIndex
fetch next getIndex 进入 @ description @ keys
while @@ fetch_status = 0
开始
声明 @ unique varchar 20
声明 @ buildIx varchar 500

- 为我们的新名称创建一个名称index。
set @ indexName = rtrim(' IX _' + replace(cast(newid() as varchar 40 )),' - '' '))

- 索引是否唯一?
SE t @ unique = ' '
if @ description like ' %unique%'
begin
set @ unique = ' unique'
end

- 汇编并执行索引创建。
set @ buildIx = ' create' + @ unique + ' index' + @ indexName +
' on' + @ tableName + ' (' + @ keys + ' )'
print @ buildIX
< span class =code-keyword> exec ( @ buildIX

getIndex 获取 @ description @ keys
end
关闭 getIndex
deallocate getIndex





如果它有效,你应该看到如下内容...



 创建 唯一 索引 IX_6A8931B2364340FCB378D9EE768C56CC  on  jim(a)
创建 索引 IX_4A471647B31E4499AE9C13179C511EBC jim(a,b)
创建 index IX_714ED1CF6FBF4CF388B4E904E8C2E665 jim(b)





最后检查一下我们我们得到了我们所希望的。



 sp_help jim 


< blockquote>



我不认为有这样的选择。您可以做什么,您可以使用查询检查TAB1上的索引,并在TAB2上创建不同的。


Hello,

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 ???

解决方案

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

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.

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.

-- 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...

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.

sp_help jim


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.


这篇关于使用SQL语句创建表的克隆,其中克隆将包含所有索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆