数据库中有类似:数据1101,数据1102.....很多表。表结构都是一样的,现在准备合并形成一个新表,新表结构就是在旧表上加一列时间。如何写sql语句?

解决方案 »

  1.   

    insert into [新表]
    select *,1101 from [数据1101]
    union
    select *,1102 from [数据1102]
    union
    select *,1103 from [数据1103]
    ……
      

  2.   

    select *
    into new_table
    from (
    select *,1101 [time] from [数据1101]
    union all
    select *,1102 from [数据1102]
    union all
    select *,1103 from [数据1103]
    ...   ) a  
      

  3.   

    --得到结果中去掉最后一个union all 
    select 'select *,'+replace(name,'数据','')+' as [time] from '+name +' union all'
    from sys.objects
    where type ='u' and name like '数据%'