由于项目是扩展原来存在的系统,于是原系统的数据要迁移到新的数据库(数据库表结构有改动)但苦于以前都没用过oracle过程等等,期间还费力查了些资料,写下来让自己以后做参考。
1,首先弄清楚各个表之间的主从关系,将主从关系列成上下层级的列表,方便查看分析;在做数据插入是,先插入没有参照其他表的表数据
2,建立数据库连接:
CREATE DATABASE LINK db_link
CONNECT TO LQPAY IDENTIFIED BY ICBCGD
USING '
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.191.6)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCL)
)
)
';
其中db_link为数据库连接名,LQPAY为原数据库用户名,ICBCGD为密码,然后host值为ip地址,ORCL为服务名
3,编写存储过程:
create or replace procedure gzsz_clda is
--LQPAY的TB06游标,用来查询、遍历车辆档案,db_link为到LQPAY的数据库连 --接,create database link db_link connect to LQPAY identified by ICBCGD using 'LQPAY';
cursor c_clda is select tb06005,tb06001,tb06002,tb06006,owneraddress,tb06003 from tb06@db_link;
--需要转移的数据的声明
v_dabh varchar2(32);
v_cxdm varchar2(2);
v_zydm varchar2(7);
v_cphm varchar2(15);
v_clsyr varchar2(60);
v_xszdz varchar2(100);
v_lxdz varchar2(100);
v_djrq varchar2(10);
v_jdlb integer;
v_clzt integer;
v_lxdh varchar2(11);
i integer;
begin
v_jdlb := 0;
i := 0;
for clda in c_clda loop
begin
v_dabh := sys_guid();
v_cxdm := clda.tb06005;
v_cphm := clda.tb06001;
v_clsyr := clda.tb06002;
v_xszdz := clda.tb06006;
v_lxdz := clda.owneraddress;
v_lxdh := clda.tb06003;
exception when others then
dbms_output.put_line(SQLERRM);
end;
--从缴费表查出车辆的最旧缴费记录的缴费日期作为车辆的登记日期,缴费经办人作为档案的经办人
begin
select tb07002,tb07003 into v_djrq,v_zydm from (select tb07001,tb07002,tb07003,row_number() over (order by tb07002 asc) as rank from tb07@db_link where tb07001=v_cphm and tb07018=v_cxdm) where rank=1;
exception when others then
v_zydm := 'AS00105';
v_djrq := '2005-01-01';
dbms_output.put_line(SQLERRM);
end;
--从封停解封表查出车辆的最新封停解封的车辆状态,作为车辆档案的车辆状态
begin
select status into v_clzt from (select status,row_number() over (order by begin_date desc) as rank from stop_pay@db_link where car_no=v_cphm and car_type=v_cxdm) a where rank=1;
if v_clzt=0 then
v_clzt := 1;
else v_clzt := 0;
end if;
exception when others then
v_clzt := 1;
dbms_output.put_line(SQLERRM);
end;
begin
insert into arch_clda(dabh,cxdm,zydm,cphm,clsyr,xszdz,lxdz,djrq,jdlb,clzt,lxdh) values(v_dabh,v_cxdm,v_zydm,v_cphm,v_clsyr,v_xszdz,v_lxdz,to_date(v_djrq,'yyyy-mm-dd'),v_jdlb,v_clzt,v_lxdh);
exception when others then
dbms_output.put_line(SQLERRM);
end;
i := i+1;
if mod(i,1000) = 0 then
commit;
end if;
end loop;
commit;
end gzsz_clda;
4,建立job让过程在服务器上运行(如果用客户端连接直接运行过程可能会出现执行一段时间后出现与服务器断开的情况)
declare
job1 integer;
begin
dbms_job.submit(job => job1,
what => 'gzsz_clda();',
next_date => sysdate);
COMMIT;
end;
http://www.iteye.com/topic/716073