日期:2014-05-17  浏览次数:20420 次

视图数据合并请教
视图数据合并请教  环境SQL200
视图vip_qyjyqkylb31
khcustname, sramt, srremark,srsortcol
--000"INTEL"木拉特, 27114.0000000, 销售收款明细, 2
--ROCA 马来西亚, 46961.8000000, 销售收款明细, 2
--TEKA 马来西亚 ,23279.0000000 ,销售收款明细, 2
--VERED A.H.LTD, 46272.0600000, 销售收款明细, 2
--巴拉圭, 5591.0000000, 销售收款明细, 2

视图vip_qyjyqkylb32
gycustname, zcamt, zcremark, zcsortcol
采购支出汇总, 1742861.4300000 ,采购支出汇总, 1
--SCC, 44000.0000000, 采购支付明细, 2
--艾春格, 305049.5000000, 采购支付明细, 2
--安利洁洁具有限公司, 25023.0000000, 采购支付明细, 2
--澳美丰五金机械加工店, 11900.0000000, 采购支付明细, 2
--宝a尔, 4680.0000000, 采购支付明细, 2
--宝b银, 150.0000000, 采购支付明细, 2

要求结果:生成新视图视图vip_qyjyqkylb33
(注vip_qyjyqkylb31和vip_qyjyqkylb32是行数不一定相同的)

khcustname, sramt, srremark,srsortcol   ,gycustname, zcamt, zcremark, zcsortcol
--000"INTEL"木拉特, 27114.0000000, 销售收款明细, 2 ,采购支出汇总, 1742861.4300000 ,采购支出汇总, 1
--ROCA 马来西亚, 46961.8000000, 销售收款明细, 2 ,--SCC, 44000.0000000, 采购支付明细, 2
--TEKA 马来西亚 ,23279.0000000 ,销售收款明细, 2         ,--艾春格, 305049.5000000, 采购支付明细, 2
--VERED A.H.LTD, 46272.0600000, 销售收款明细, 2 ,--安利洁洁具有限公司, 25023.0000000, 采购支付明细, 2
--巴拉圭, 5591.0000000, 销售收款明细, 2         ,--澳美丰五金机械加工店, 11900.0000000, 采购支付明细, 2
NULL, 0 ,NULL,NULL,                                       --宝a尔, 4680.0000000, 采购支付明细, 2
NULL, 0 ,NULL,NULL,                   

------解决方案--------------------

if OBJECT_ID('tempdb..#temp1') is not null
   drop table #temp1

if OBJECT_ID('tempdb..#temp2') is not null
   drop table #temp2   

select khcustname, sramt, srremark,srsortcol,IDENTITY(int,1,1) as id into #temp1
from vip_qyjyqkylb31


select gycustname, zcamt, zcremark, zcsortcol,IDENTITY(int,1,1) as id  into #temp2
from vip_qyjyqkylb32


select khcustname, isnull(sramt,0) as sramt, srremark,srsortcol,
       gycustname, isnull(zcamt,0) as zcamt, zcremark, zcsortcol
from 
(
select ID from #temp1
union
select id from #temp2
)t
left join #temp1 t1
       on t.id = t1.id