select id=identity(int),a,b into #a from aaa
select id=identity(int),e,f into #b from bbb
select a=isnull(a.a,''),b=isnull(a.b,''),e=isnull(b.e,''),f=isnull(b.f,'')
from #a a full join #b b on a.id=b.id
drop table #a,#b

解决方案 »

  1.   

    增加个id让每条记录对应,然后full join
      

  2.   

    insert into #t1 select identity(int,1,1) as id,a,b from 表1
    insert into #t2 select identity(int,1,1) as id,e,f from 表2select a,b,e,f from #t1 right join #t2 on #t1.id=#t2.id
      

  3.   

    欲求结果    a  b  e  f
                c  d  g  h
                      i  j
      

  4.   

    select a=isnull(a.a,''),b=isnull(a.b,''),e=isnull(b.e,''),f=isnull(b.f,'')
     from aaa,bbb 
      

  5.   

    select a=isnull(a.a,''),b=isnull(a.b,''),e=isnull(b.e,''),f=isnull(b.f,'')
     from aaa a,bbb b
      

  6.   

    --测试--测试数据
    create table aaa(a varchar(10),b varchar(10))
    insert aaa select 'a','b'
    union  all select 'c','d'create table bbb(e varchar(10),f varchar(10))
    insert bbb select 'e','f'
    union  all select 'g','h'
    union  all select 'i','j'
    go--查询
    select id=identity(int),a,b into #a from aaa
    select id=identity(int),e,f into #b from bbb
    select a=isnull(a.a,''),b=isnull(a.b,''),e=isnull(b.e,''),f=isnull(b.f,'')
    from #a a full join #b b on a.id=b.id
    drop table #a,#b
    go--删除测试
    drop table aaa,bbb/*--测试结果a          b          e          f          
    ---------- ---------- ---------- ---------- 
    a          b          e          f
    c          d          g          h
                          i          j(所影响的行数为 3 行)
    --*/
      

  7.   

    欲求结果    a  b  e  f
              c  d  g  h
                    i  j
      

  8.   

    --邹建的对的啊:
    --示例代码:
    declare @aaa table(a char(2),b char(2))
    insert into @aaa
    select 'c','d' union all
    select 'cc','dd'declare @bbb table(e char(2),f char(2))
    insert into @bbb
    select 'g','h' union all
    select 'i','j' union all
    select 'gg','hh' union all
    select 'ii','jj'select [id]=identity(int),a,b into #a from @aaa
    select [id]=identity(int),e,f into #b from @bbb
    select a=isnull(a.a,''),b=isnull(a.b,''),e=isnull(b.e,''),f=isnull(b.f,'')
    from #a a full join #b b on a.id=b.iddrop table #a,#b--测试结果:
    a    b    e    f    
    ---- ---- ---- ---- 
    c    d    g    h 
    cc   dd   i    j 
              gg   hh
              ii   jj(所影响的行数为 4 行)