sql server中复制表结构,如果将约束、相关性也一起复制过来
1、
select top 0 * into bfrom a
declare ap scroll cursor forselect name from sysobjects where xtype='U'declare @tab varchar(200),@sql varchar(5000)open apfetch first from ap into @tabwhile(@@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 @tabendclose apdeallocate ap