将一个数据库中表的数据导入另一个数据库(DB2)

将一个数据库中的数据导入另一个数据库(DB2)

我这里举得例子是使用的DB2数据库,其他数据库思路也是这样啦!

1、从db2 数据库中将表中的数据导入本地的excel中

export to d:\mytest.xls of del modified by nochardel coldel0x09 select * from IOUT_BUSI_YWDJMX_TEMP

2、在需要导入的数据库建立一个临时表

--创建临时表
CREATE TABLE NBADV.l_hzcitywa
(

fhcode varchar(20),
dhcode varchar(20),
away DECIMAL(19, 4)

)
DATA CAPTURE NONE
IN USERSPACE1@

ALTER TABLE NBADV.l_hzcitywa
LOCKSIZE ROW
APPEND OFF
NOT VOLATILE@

3、将excel另存为csv格式的文件,导入到数据库的临时表中

--将数据导入临时表
import from "D:\hz.csv" OF DEL INSERT INTO nbadv.l_hzcitywa@

4、对数据进行处理

--查出重复的数据
select distinct a.DELI_UNIT_CD,c.fhcode,a.ARRI_UNIT_CD,c.dhcode,count(c.away) from ST_I_STD_MILEAGE a 
inner join nbadv.l_hzcitywa c on a.DELI_CITY_NM=c.fhcode and a.ARRI_CITY_NM=c.dhcode
group by a.DELI_UNIT_CD,c.fhcode,a.ARRI_UNIT_CD,c.dhcode
having count(*)>1

--删除掉重复的数据
delete from nbadv.l_hzcitywa where dhcode in ('北京','东莞','拉萨','天水','酒泉','平凉','庆阳','铜川','咸阳','烟台')@

5、处理后的数据通过db2存储过程将所需要的数据更新到第二个数据库的表中,这里关键是找到临时表与需要更新数据的表中的关联关系;

--创建存储过程,更新合同里程
drop procedure L_getcityway@

create procedure L_getcityway(
in v_personId    int
)
language sql
dynamic result sets 0

begin
declare sqlcode int;
declare sqlstate char(5);
declare v_errcode int default 0;


--更新合同里程
merge into ST_I_TRANS_MILEAGE a
using (
select distinct a.DELI_UNIT_CD,c.fhcode,a.ARRI_UNIT_CD,c.dhcode,c.away from ST_I_STD_MILEAGE a 
inner join nbadv.l_hzcitywa c on a.DELI_CITY_NM=c.fhcode and a.ARRI_CITY_NM=c.dhcode and c.fhcode= '渭南'
)b
on a.DELI_STORE_CD=b.DELI_UNIT_CD and a.ARRI_STORE_CD=b.ARRI_UNIT_CD
when matched then 
update set a.ACTUAL_MILEAGE=b.away;

--计算差异
update ST_I_TRANS_MILEAGE set DIFFERENCE=(ACTUAL_MILEAGE-STD_MILEAGE)*100.000000/STD_MILEAGE where DELI_STORE_CD=2061000101 and STD_MILEAGE is not null and STD_MILEAGE<>0;


--更新备注
update ST_I_TRANS_MILEAGE set REMARK='' where DIFFERENCE>10 and DELI_STORE_CD in ('2061000101','2061000102','2061000103','2061000104','2061000105');

return v_errcode;

end@

到此,已经实现从第一个数据库的表中的数据更新到第二个数据库中了;

posted @ 2015-09-02 17:50  HalfWater  阅读(7681)  评论(0编辑  收藏  举报