多表连接有关问题,

多表连接问题,在线等!!!!!!!!!!!!!!!!!
有多个表结构如下:
表A:
CODE QTY NO
3337 4423.4000000000 1
9371 4202.5000000000 2
3327 3440.1000000000 3
3342 2517.8000000000 4

表B
CODE QTY NO
14253 97.9000000000 1
14256 20 2

要将两个表连接起来得到如下表

CODE QTY NO
3337 4423.4000000000 1
9371 4202.5000000000 2
3327 3440.1000000000 3
3342 2517.8000000000 4
14253 97.9000000000 1
14256 20 2

我用UNION连接得到的表NO的顺序是乱的.要连接多个这样的表怎么实现呢?

------解决方案--------------------
多个表用UNION连接完了以后可以用 order by 排序的

如果想完全按照A B C 表的顺利可以插入到一个带自增长列的临时表,代码就不写了,这里代码写的好的牛人太多了
------解决方案--------------------
SQL code

--测试
declare @t1 table(code int ,qyt float(20),no int)
insert @t1 select 3337,4423.4000,1
insert @t1 select 9371,4202.5000,2
insert @t1 select 3327,3440.1000,3
insert @t1 select 3342,2517.8000,4
declare @t2 table(code int ,qyt float(20),no int)
insert @t2 select 14253,97.4000,1
insert @t2 select 14256,20,2

select * from @t1
union all
select * from @t2
/*
code        qyt           no
----------- ------------- -----------
3337        4423.4        1
9371        4202.5        2
3327        3440.1        3
3342        2517.8        4
14253       97.4          1
14256       20            2

(6 行受影响)
*/

------解决方案--------------------
#若有多个表,只要表结构相同一直
union all 就ok了
------解决方案--------------------
UNION ALL 正解
------解决方案--------------------
select * from table1
union all
select * from table2

union all表示两表部连接,包括相同值,union不包括相同值
------解决方案--------------------
agree

UNION ALL

------解决方案--------------------
UNION ALL
------解决方案--------------------
SQL code
--原始数据:@A
declare @A table(CODE int,QTY decimal(14,10),NO int)
insert @A
select 3337,4423.4000000000,1 union all
select 9371,4202.5000000000,2 union all
select 3327,3440.1000000000,3 union all
select 3342,2517.8000000000,4
--原始数据:@B
declare @B table(CODE int,QTY decimal(12,10),NO int)
insert @B
select 14253,97.9000000000,1 union all
select 14256,20,2

select * into #temp
from
(
select * from @A
union all
select * from @B
) a

select * from #temp
--顺序没有变

--删除测试
drop table #temp

------解决方案--------------------
SQL code

select [CODE], [QTY] ,[NO]
from 
(
select [CODE], [QTY] ,[NO],1 as tab from t1
union all 
select [CODE], [QTY] ,[NO],2 as tab from t2
)t
order by tab asc,[NO] asc

------解决方案--------------------
在表进行链接时,加一个自增的id,然后在列出记录时按照id排列

SQL code

declare @A table(CODE int,QTY decimal(14,10),NO int)
insert @A
select 3337,4423.4000000000,1 union all
select 9371,4202.5000000000,2 union all
select 3327,3440.1000000000,3 union all
select 3342,2517.8000000000,4

declare @B table(CODE int,QTY decimal(12,10),NO int)
insert @B
select 14253,97.9000000000,1 union all
select 14256,20,2



select identity(int,1,1) as id,* into #temp
from
(
select * from @A
union all
select * from @B
) a

select CODE,QTY,NO from #temp order by id