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

请问这段备份数据表的PL/SQL代码该如何写?谢谢!
比如, 有如下table
tblUser
字段: fldUserID(PK), fldUserName, fldCreated_ON.

我希望备份这张表的老的记录到备份表中,同时删除原表对应记录。

备份表:
tblUser_ARC
字段: fldUserID(PK), fldUserName, fldCreated_ON, fldArc_Date

要求
1.只备份fldCreated_ON<sysdate-60的记录
2. 由于 tblUser中记录太多,为了性能,需要做到每次事务只做500条的插入、删除、commit操作。就是说,先从原表中select500条fldCreated_ON最早的记录,把它insert到tblUser_ARC中,再从tblUser中delete掉这500条记录,让后提交事务;接着循环上面操作,直到fldCreated_ON<sysdate-60的记录在tblUser表中不存在。



请问PL/SQL代码该如何写?谢谢!

------解决方案--------------------
不就是
insert into tblUser_ARC(你要的字段) 
select 你要的字段 from tbluser 
where fldcreated_on<sysdate-60 and rowid<=500;

delete from tbluser where fldcreated_on<sysdate-60 where rowid<=500;

吗?
------解决方案--------------------
如果考虑性能的话,楼主这种方法也不是性能最佳的啊,你可以试试用以下方法:

insert into tbluser_arc
select fldUserID(PK), fldUserName, fldCreated_ON, sysdate from tbluser
where fldcreated_on <sysdate-60; 

commit;
--创建个临时表
create table tbluser_tmp as
select * from tbluser;
--
truncate table tbluser;
--
insert into tbluser
select * from tbluser_tmp where fldcreated_on >=sysdate-60;
commit;