数据库
首页 > 数据库> > oracle时间查询 oracle定时任务备份数据

oracle时间查询 oracle定时任务备份数据

作者:互联网

--每个月5号
SELECT TRUNC(SYSDATE, 'MM') + 4 + 2 / 24 FROM dual ;
SELECT (TRUNC(SYSDATE,'YYYY')-INTERVAL '1' YEAR),(TRUNC(SYSDATE,'YYYY')) FROM dual;
select add_months(sysdate,-24) as 前年的今天, add_months(sysdate,-1) as 前一个月的今天 from dual;
select add_months((add_months(sysdate,-24)),-1) as  前年的前一个月的今天 from dual;
select add_months((LAST_DAY(sysdate)),-1) as 最后一天 from dual;
select add_months((add_months((TRUNC(sysdate, 'mm')),-24)),-1)  as 第一天,
add_months((TRUNC(sysdate, 'mm')),-24) as  第二个月的第一天 from dual;
select add_months((add_months((TRUNC(sysdate, 'mm')),-24)),-1) as  前年的前一个月的今天 from dual;
SELECT * FROM WIP_LOT WHERE CREATED >
add_months((add_months(sysdate,-24)),-1);
--创建存储过程

–下面才是正式sql

create or replace procedure copy_wlh  
as  
str_sql varchar2(2000);  
str_sql2 varchar2(2000);  
begin  
--str_sql:='INSERT into "WIP_LOT_HIS_BACK" SELECT * FROM WIP_LOT_HIS WHERE TRANS_TIME >= --add_months((add_months((TRUNC(sysdate,''mm'')),-24)),-1)  
--AND TRANS_TIME < add_months((TRUNC(sysdate, ''mm'')),-24)'
str_sql:='INSERT into "WIP_LOT_copy1" SELECT * FROM WIP_LOT WHERE CREATED > add_months((add_months((TRUNC(sysdate,''mm'')),-24)),-1)   AND
CREATED <add_months((TRUNC(sysdate, ''mm'')),-24)';
str_sql2:='DELETE FROM WIP_LOT_copy1';
execute immediate str_sql; 
execute immediate str_sql2; 
exception  
when others then  
null; 
end ;
--运行存储过程
begin
   proc_test(add_months((add_months(sysdate,-24)),-1));   
end;
--创建job
declare      
   my_job number;
begin
   dbms_job.submit(my_job, 'proc_test;', sysdate, 'sysdate+10/24*60*60');--每10插入一条记录
end;
--查询job
select job, what from dba_jobs;
--运行
begin
   dbms_job.run(6);
end; 
--下次
 select job,next_date,what from dba_jobs
 where job='6';
 --删除
 begin
   dbms_job.remove(6);
end;  

标签:sysdate,24,备份,months,add,dual,oracle,定时,TRUNC
来源: https://blog.csdn.net/zhouzhou__java/article/details/122192880