日期:2014-05-18  浏览次数:20422 次

怎樣將兩個行數不同結構不一樣的表合併起來,行數不足的表補空?
比如表A
n1   n2   n3   n4
1     2     3     4
2     3     4     5
a     b     c     d
表B
f1   f2   f3
2     e     f
w     f     g
合併後:
n1   n2   n3   n4   f1   f2   f3
1     2     3     4     2     e     f
2     3     4     5     w     f     g
a     b     c     d
用sql查詢實現,不准用臨時表

------解决方案--------------------
UP.......
------解决方案--------------------
declare @a table(n1 varchar(20), n2 varchar(20), n3 varchar(20), n4 varchar(20))
insert @a select '1 ', '2 ', '3 ', '4 '
union all select '2 ' , '2 ' , '3 ' , '4 '
union all select 'a ' , 'b ' , 'c ' , 'd '


declare @b table(f1 varchar(20), f2 varchar(20), f3 varchar(20))
insert @b select '2 ', 'e ', 'f '
union all select 'w ' , 'f ' , 'g '

select n1,n2,n3,n4,f1,f2,f3 from
(
select *,id=(select count(1) from (select *,checksum(*) x from @a)bb where x <=aa.x) from (select *,checksum(*) x from @a) aa
)xx
left join
(
select *,id=(select count(1) from (select *,checksum(*) x from @b)bb where x <=aa.x) from (select *,checksum(*) x from @b) aa
)yy
on xx.id=yy.id