在数据库里有几十个表怎么拷贝其中二十个表到另一个新建的空数据库?
因为有的表我不想要数据,有的又想要原来的数据,所以
要有原来的表结构,和有表结构也有数据,两种

解决方案 »

  1.   

    使用游标循环表..循环sysobjects的xtype='U'为用户表..
    再判断name是否为你想保留数据的表..
    如果是,,则select * into 新库.dbo.表 from 表
    不是,,则.select * into 新库.dbo.表 from 表 where 1=2
      

  2.   

    exec sp_msforeachtable 'select * into [web].[dbo].[?] from ?'
      

  3.   

    直接使用DTS(2000)或SSIS(2005)导入导出.
      

  4.   

    select * into p..t2 from t1  --有结构有数据
    select * into p..t2 from t1 where 1=2 --有结构无数据
      

  5.   

    exec sp_msforeachtable 'select *  into web.?  from ?'
      

  6.   

    因为有的表我不想要数据,有的又想要原来的数据?
    -----------
    SQL没有人工智能化
    只有判断条件
      

  7.   

    use 旧库
    go
    exec sp_msforeachtable 'select *  into 新库.?  from ? where exists(select 1 from tableName where Name=''?'')'--子查询里输入条件判断就行了
      

  8.   

    按照老乌龟说的,如果是2K,用DTS.如果是2K5,用SSIS.
    导入有两种选择方式,一种是全表导入,一种是按照你需要的查询条件导入.都满足你的要求.
      

  9.   

    我根据大家的提示写了下面这个,可转过去的表怎么都没有主键和默认值了啊,之前在表上建立的触发器也没有了, 怎么一起转过去啊?declare   @name   varchar(40)   
        
      declare   t_cursor   cursor   for   
      select   name   from   sysobjects   where   type='U'   
        
      open   t_cursor   
        
      fetch   next   from   t_cursor   into   @name   
        
      while   @@fetch_status=0   
      begin   
              if exists(select 1 from sysobjects where name=@name and name in ('OAInfo'))
              exec('select * into test..'+@name+'   from   '+@name)
              else if exists(select 1 from sysobjects where name=@name and name in ('Address'))
              exec('select * into test..'+@name+'  from   '+@name+' where 1<>1')
              fetch   next   from   t_cursor   into   @name   
      end   
        
      close   t_cursor   
      deallocate   t_cursor   
        
      go
      

  10.   

    直接使用DTS(2000)或SSIS(2005)导入导出. 
    我才不用那个呢, 那样什么都学不会
      

  11.   

    我什么都不懂,希望能学到点有用的东西, 哪天没有DTS了怎么办...
      

  12.   

    用DTS吧  方便  
      

  13.   


    手工导入---有数据的
    select * into 新库名.dbo.newtab from tab--没有数据的
    生成脚本后,再新库里面运行