日期:2014-05-18  浏览次数:20615 次

sql server中复制表结构,如果将约束、相关性也一起复制过来
1、
SQL code
select top 0 * into b
from a

这种脚本复制过来的不包括约束,我现在需要将表结构,对应的约束,相关性等都复制过来该怎么办?

2、Sql Server2000企业管理器的表结构生成功能是否支持命令行,我现在只想把库里面的部分表结构导出成sql脚本,难道得一个个的去用手点么?



------解决方案--------------------
http://blog.csdn.net/fredrickhu/article/details/4669511
------解决方案--------------------
给你一段动态产生primary key的脚本,
SQL code

declare ap scroll cursor for
select name from sysobjects where xtype='U'

declare @tab varchar(200),@sql varchar(5000)

open ap
fetch first from ap into @tab
while(@@FETCH_STATUS<>-1)
begin
 select @sql='alter table ['+@tab+'] add constraint ['
 +name+'] '
 from sys.objects
 where parent_object_id=object_id(@tab) and [type]='PK'

 select @sql=@sql+' primary key('+
 (select cast(
 (select name+','
 from (select distinct c.name
 from sys.indexes a
 inner join sys.index_columns b
 on a.[object_id]=b.[object_id]
 inner join sys.columns c
 on b.[object_id]=c.[object_id]
 and b.index_column_id=c.column_id
 where a.[object_id]=object_id(@tab)
 and a.is_primary_key=1) t
 for xml path('')) as varchar(20)))

 select @sql=left(@sql,len(@sql)-1)+') '
 
 -- 打印
 print @sql

 fetch next from ap into @tab
end

close ap
deallocate ap