表中的字段不同? 这样写也行啊select id,相同的字段列表,仅表A存在的字段列表,仅表B存在的字段列表(用字段名=0的形式)
from 表A
union all
select id,相同的字段列表,仅表A存在的字段列表(用字段名=0的形式),仅表B存在的字段列表
from 表B
order by id

解决方案 »

  1.   

    --示例declare @a table(id int,name varchar(10),sex varchar(10))
    insert @a select 1,'张三','男'
    union all select 1,'张三','女'
    union all select 2,'张三','女'declare @b table(id int,name varchar(10),age int)
    insert @a select 1,'张三',10
    union all select 2,'张三',30
    union all select 3,'张三',40--查询
    select id,name,sex,age=0
    from @a
    union all
    select id,name,sex='',age
    from @b
    order by id/*--测试结果id          name       sex        age         
    ----------- ---------- ---------- ----------- 
    1           张三         男          0
    1           张三         女          0
    1           张三         10         0
    2           张三         30         0
    2           张三         女          0
    3           张三         40         0(所影响的行数为 6 行)
    --*/
      

  2.   

    把测试的表纪录写好。
    create table A(ID int);
    create table B(ID int)
    insert into a values (1)
    insert into a values (3)
    insert into a values (5)
    insert into b values (2)
    insert into b values (4)
    insert into b values (6)drop table a;
    drop table b;
      

  3.   

    Select * from (
    select id from A
    union all
    Select id from B) as C
    order by id
      

  4.   

    --或者是动态的处理--测试数据
    create table ta(id int,name varchar(10),sex varchar(10))
    insert ta select 1,'张三','男'
    union all select 1,'张三','女'
    union all select 2,'张三','女'create table tb(id int,name varchar(10),age int)
    insert tb select 1,'张三',10
    union all select 2,'张三',30
    union all select 3,'张三',40
    go--查询(从系统表取字段名,这样你可以任意修改两个表的字段来测试)--定义查询参数
    declare @tb1 sysname,@tb2 sysname
    select @tb1='ta',@tb2='tb' --要查询ta,tb两个表--生成处理语句
    declare @s1 varchar(8000),@s2 varchar(8000)
    select @s1='',@s2=''
    select @s1=@s1+','+a,@s2=@s2+','+b
    from(
    select top 100 percent 
    a=case 
    when a.name is null then '['+b.name+']='''''
    else '['+a.name+']' end,
    b=case 
    when b.name is null then '['+a.name+']='''''
    else '['+b.name+']' end
    from(
    select name from syscolumns where id=object_id(@tb1)
    )a full join(
    select name from syscolumns where id=object_id(@tb2)
    )b on a.name=b.name
    order by case a.name when b.name then 0 else 1 end,b.name
    )a
    select @s1=stuff(@s1,1,1,''),@s2=stuff(@s2,1,1,'')
    exec('
    select '+@s1+' from ['+@tb1+']
    union all
    select '+@s2+' from ['+@tb2+']
    order by id --因为楼主有个order by id,所以要求查询的两个表中都要有id字段
    ')
    go--删除测试
    drop table ta,tb/*--测试结果id          name       sex        age         
    ----------- ---------- ---------- ----------- 
    1           张三         男          0
    1           张三         女          0
    1           张三                    10
    2           张三                    30
    2           张三         女          0
    3           张三                    40
    --*/