日期:2014-05-18 浏览次数:20514 次
--测试 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
------解决方案--------------------
--原始数据:@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
------解决方案--------------------
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排列
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