我在PK数据库中存了很多结构相同的表,现在想把它们合并,并且写到另一个数据库analysis中的zb中,并加一个ID字段,请问该如何处理?

解决方案 »

  1.   


    select id = identity(int,1,1),*
      into B..newtable
    from (
        select a1,a2,a3 from A..t1
        union all
        select a1,a2,a3 from A..t2
        union all
        select a1,a2,a3 from A..t3
    )t
      

  2.   

    直接union all 没办法写到别的表啊。。
      

  3.   

    麻烦问一下括号后面的那个t是什么?A、B是数据库的名字吧?select id = identity(int,1,1),*
      into analysis.newtable
    from (
        select * from PKCorpusFrequence.C20001104
        union all
        select * from PKCorpusFrequence.C20001105
        union all
        select * from PKCorpusFrequence.C20001106
    )t
    为什么会显示对象名  'PKCorpusFrequence.C20001104' 无效。
      

  4.   

    t是别名 'PKCorpusFrequence.C20001104' 无效是因为你数据库中没有这个表
      

  5.   

    可以尝试 PKCorpusFrequence..C20001104
      

  6.   

    select id = identity(int,1,1),*
      into analysis..newtable
    from (
      select * from PKCorpusFrequence..C20001104
      union all
      select * from PKCorpusFrequence..C20001105
      union all
      select * from PKCorpusFrequence..C20001106
    )t
      

  7.   


    if object_id('tb1') is not null drop table tb1
    GO
    create table tb1(id int identity,val int)
    insert into tb1
    select 1 union all
    select 2 
    select * from tb1if object_id('tb2') is not null drop table tb2
    GO
    create table tb2(id int identity,val int)
    insert into tb2
    select 21 union all
    select 22 select * from tb2if object_id('tb3') is not null drop table tb3
    GO
    create table tb3(id int identity,val int)
    insert into tb3
    select 331 union all
    select 332 select * from tb3--将tb1  tb2数据插入tb3中
    insert into tb3(val)
    select val from tb1 union all
    select val from tb2select * from tb3
    /* 结果(2 行受影响)
    id          val
    ----------- -----------
    1           1
    2           2(2 行受影响)
    (2 行受影响)
    id          val
    ----------- -----------
    1           21
    2           22(2 行受影响)
    (2 行受影响)
    id          val
    ----------- -----------
    1           331
    2           332(2 行受影响)(4 行受影响)id          val
    ----------- -----------
    1           331
    2           332
    3           1
    4           2
    5           21
    6           22(6 行受影响)
    */