select * into copytable from table1

解决方案 »

  1.   

    --参考:
    http://community.csdn.net/Expert/topic/4279/4279864.xml?temp=.3511774
      

  2.   

    --数据可以参考:数据库导出工具l.exe 工具,或者用存储过程导,
    --参考:
    -- ======================================================--根据表中数据生成insert语句的存储过程--建立存储过程,执行 proc_insert 表名--感谢Sky_blue-- ====================================================== CREATE proc proc_insert (@tablename varchar(256))asbegin       set nocount on       declare @sqlstr varchar(4000)       declare @sqlstr1 varchar(4000)       declare @sqlstr2 varchar(4000)       select @sqlstr='select ''insert '+@tablename       select @sqlstr1=''       select @sqlstr2=' ('       select @sqlstr1= ' values ( ''+'       select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case --     when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'       when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'       when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'       when a.xtype =61  then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'       when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'       when a.xtype =62  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'       when a.xtype =56  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'       when a.xtype =60  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'       when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'       when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'       when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'       when a.xtype =59  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'       when a.xtype =58  then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'       when a.xtype =52  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'       when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'       when a.xtype =48  then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'--     when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'       when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'       else '''NULL'''       end as col,a.colid,a.name       from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and  a.xtype <>36       )t order by colid              select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename--  print @sqlstr       exec( @sqlstr)       set nocount offendGO
      

  3.   

    T-SQL 存储过程: (修订版) 根据基本表结构及其数据生成 INSERT INTO ... 的 SQL 
    http://microshaoft.cnblogs.com/archive/2005/08/06/208876.html
      

  4.   

    /*--数据库数据复制 将一个数据库中的数据复制到另一个数据库
     如果某列在目标数据库中为标识列,将不会被复制 适用范围:数据库结构发生了变化,想将旧数据库进行升级
      这样就可以根据新的数据库结构创建一个空库,然后
      将旧数据库的所有数据复制到新库中--邹建 2003.10(引用请保留此信息)--*//*--调用示例 exec p_copydb 'bns_aa','bns_new'
     exec p_copydb 'acc_五医','acc_演示数据8'
    --*/
    create  proc p_copydb
    @o_dbname sysname,  --要复制数据的数据库--源数据库
    @n_dbname sysname  --接收数据的数据库--目标数据库
    as
    declare @sql nvarchar(4000)--禁用约束/触发器,防止复制时的数据冲突
    set @sql='declare #tbc cursor for select name
     from '+@n_dbname+'..sysobjects where xtype=''U'' and status>=0'
    exec(@sql)declare @tbname sysname
    open #tbc
    fetch next from #tbc into @tbname
    while @@fetch_status=0
    begin
     set @sql='alter table '+@n_dbname+'..['+@tbname+'] NOCHECK CONSTRAINT ALL'
     exec(@sql)
     set @sql='alter table '+@n_dbname+'..['+@tbname+'] disable trigger ALL'
     exec(@sql)
     fetch next from #tbc into @tbname
    end
    close #tbc--复制数据
    declare @sql1 varchar(8000)
    set @sql='declare #tb cursor for select a.name from '
     +@o_dbname+'..sysobjects a inner join '
     +@n_dbname+'..sysobjects b on a.name=b.name
      where a.xtype=''U'' and b.xtype=''U'''
    exec(@sql)
    open #tb
    fetch next from #tb into @tbname
    while @@fetch_status=0
    begin
     select @sql1=''
      ,@sql='select @sql1=@sql1+'',[''+a.name+'']'' from(
      select name from '+@o_dbname+'..syscolumns where id in 
      (select id from '+@o_dbname+'..sysobjects where name='''+@tbname+''')
     ) a inner join (
      select name from '+@n_dbname+'..syscolumns where status<>0x80 and id in 
      (select id from '+@n_dbname+'..sysobjects where name='''+@tbname+''')
     ) b on a.name=b.name'
     exec sp_executesql @sql,N'@sql1 nvarchar(4000) out',@sql1 out select @sql1=substring(@sql1,2,8000)
     exec('insert into '+@n_dbname+'..['+@tbname+']('+@sql1
      +') select '+@sql1+' from '+@o_dbname+'..['+@tbname+']')
     if @@error<>0
      print('insert into '+@n_dbname+'..['+@tbname+']('+@sql1
       +') select '+@sql1+' from '+@o_dbname+'..['+@tbname+']')
     fetch next from #tb into @tbname
    end
    close #tb
    deallocate #tb--数据复制完成后启用约束
    open #tbc
    fetch next from #tbc into @tbname
    while @@fetch_status=0
    begin
     set @sql='alter table '+@n_dbname+'..['+@tbname+'] CHECK CONSTRAINT ALL'
     exec(@sql)
     set @sql='alter table '+@n_dbname+'..['+@tbname+'] enable trigger ALL'
     exec(@sql)
     fetch next from #tbc into @tbname
    end
    close #tbc
    deallocate #tbc
      

  5.   

    回复人: xiaomeixiang(小山羊:) 咩…咩……) ( ) 信誉:100  2005-09-19 19:13:00  得分: 0  
     
     
       select * into copytable from table1
      
     
    这个以经可以了。