假设有A、B、C三张表,表的数据结构相同。再新建一个数据结构相同的表D,然后把 A,B,C三张表的数据都插入到D中, 顺序是 A1,B1,C1;A2,B2,C2……请问怎么最优化。

解决方案 »

  1.   

    Select ID = Identity(Int, 1, 1) , * Into #A From A
    Select ID = Identity(Int, 1, 1) , * Into #B From B
    Select ID = Identity(Int, 1, 1) , * Into #C From CSelect * Into D
    From
    (Select * From #A 
    Union All 
    Select * From #B
    Union All 
    Select * From #C) T
    Order By ID
      

  2.   

    将鱼的改改才行吧
    Select ID = Identity(Int, 1, 3) , * Into #A From A
    Select ID = Identity(Int, 2, 3) , * Into #B From B
    Select ID = Identity(Int, 3, 3) , * Into #C From Cinsert D select 不含id的列的列表 
    From
    (Select * From #A 
    Union All 
    Select * From #B
    Union All 
    Select * From #C) T
    Order By ID
      

  3.   

    --union all里加编号排序
    create table A(col1 int,col2 int,col3 int)
    create table B(col1 int,col2 int,col3 int)
    create table C(col1 int,col2 int,col3 int)insert A select 1,2,3
    union all select 4,5,6insert B select 7,8,9
    union all select 10,11,12insert C select 13,14,15
    union all select 16,17,18select * into D from
    (
    select col1,col2,col3,bh=1 from A
    union all
    select col1,col2,col3,bh=2 from B
    union all
    select col1,col2,col3,bh=3 from C
    )t
    order by bhselect col1,col2,col3 from Ddrop table A,B,Ccol1        col2        col3        
    ----------- ----------- ----------- 
    1           2           3
    4           5           6
    7           8           9
    10          11          12
    13          14          15
    16          17          18
      

  4.   

    谢谢鱼,谢谢wgzaaa() ,鱼的思路,wgzaaa()的实现。
    不知道有没有别的方法.
      

  5.   

    哦,还有bill024(咖啡熊) 。呵呵。我这里数据量可能非常大。各个大哥能不能说下效率的问题。。
      

  6.   

    wgzaaa() ( ) 信誉:100    Blog   加为好友  2007-6-12 23:05:28  得分: 40  
     
     
       
    将鱼的改改才行吧
    Select ID = Identity(Int, 1, 3) , * Into #A From A
    Select ID = Identity(Int, 2, 3) , * Into #B From B
    Select ID = Identity(Int, 3, 3) , * Into #C From Cinsert D select 不含id的列的列表 
    From
    (Select * From #A 
    Union All 
    Select * From #B
    Union All 
    Select * From #C) T
    Order By ID------------如果D表是新建的話,加上ID列也無所謂, 所以在新建表的時候就沒有去掉這個ID列。