search
HomeDatabaseMysql TutorialOracle日志定期清理存储过程

常要oracle数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作,这时

常要Oracle数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作,这时需要用到一个函数dbms_job.submit,来完成Oracle定时器Job时间的处理上。使用dbms_job.submit这个函数,我们只需要考虑两个事情:安排某一任务,和定制一个执行任务的时间点。但最重要也是最棘手的事情,我认为还是确定一个执行任务的时间点。时间点确定了,其他的事情就好办了。下面是函数dbms_job.submit使用方法:

Java代码 

1.  dbms_job.submit( job out binary_integer,

2.  what in archar2,

3.  next_date in date,

4.  interval in varchar2,

5.  no_parse in boolean)


其中:
●job:输出变量,是此任务在任务队列中的编号;
●what:执行的任务的名称及其输入参数;
●next_date:任务执行的时间;
●interval:任务执行的时间间隔。
其中Interval这个值是决定Job何时,被重新执行的关键;当interval设置为null时,该job执行结束后,就被从队列中删除。假如我们需要该job周期性地执行,则要用‘sysdate+m’表示。如何更好地确定执行时间的间隔需要我们掌握一个函数TRUNC。

1.TRUNC(for dates)
TRUNC函数为指定元素而截去的日期值。
其具体的语法格式如下:
TRUNC(date[,fmt])
其中:
date 一个日期值
fmt 日期格式,该日期将由指定的元素格式所截去。忽略它则由最近的日期截去
下面是该函数的使用情况:
1)按年截尾
select  TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'yyyy')  from dual
-----------------------------------------------------------
2008-1-1
2)按月截尾
select  TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'mm')  from dual
--------------------------------------------------------
2008-3-1
3)按日截尾
select  TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'dd')  from dual
----------------------------------------------------------------------
2008-3-1
4)按时截尾
select  TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'hh')  from dual
----------------------------------------------------------------------
2008-3-1 8:00:00
5)按分截尾
select  TRUNC(TO_DATE('2008-03-01 08:23','yyyy-mm-dd hh:mi'),'mi')  from dual
----------------------------------------------------------------------
2008-3-1 8:23:00

2.确定执行时间间隔
1) 每分钟执行
Interval => TRUNC(sysdate,'mi') + 1 / (24*60) 或Interval => sysdate+1/1440
2) 每天定时执行
例如:每天的凌晨2点执行
Interval => TRUNC(sysdate) + 1 +2 / (24)
3) 每周定时执行
例如:每周一凌晨2点执行
Interval => TRUNC(next_day(sysdate,2))+2/24 --星期一,一周的第二天

Interval => TRUNC(next_day(sysdate,'星期一'))+2/24
4) 每月定时执行
例如:每月1日凌晨2点执行
Interval =>TRUNC(LAST_DAY(SYSDATE))+1+2/24
5) 每季度定时执行
例如每季度的第一天凌晨2点执行
Interval => TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 2/24
6) 每半年定时执行
例如:每年7月1日和1月1日凌晨2点
Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),6)+2/24
7) 每年定时执行
例如:每年1月1日凌晨2点执行
Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),12)+2/24


3.实例
这里提供了一个简单的例子,主要是完成在每一个时间间隔内向一个表中插入一条记录
  1)创建测试表 

 

Java代码 

1.    

2.  SQL>   create   table   test(id number,cur_time   date);   

3.    表已创建。 

4.  ----建sequence 

5.  CREATE  SEQUENCE test_sequence 

6.  INCREMENT  BY   1    --  每次加几个  

7.  START  WITH   1     --  从1开始计数  

8.  NOMAXVALUE     --  不设置最大值  

9.  NOCYCLE      --  一直累加,不循环  

10. CACHE  10 ; 

 

--建触发器代码为:

Java代码 

1.  create or replace trigger tri_test_id 

2.    before insert on test   --test 是表名 

3.    for each row 

4.  declare 

5.    nextid number; 

6.  begin 

7.    IF :new.id IS NULLor :new.id=0 THEN --id是列名 

8.      select test_sequence.nextval --SEQ_ID正是刚才创建的 

9.      into nextid 

10.     from sys.dual; 

11.     :new.id:=nextid; 

12.   end if; 

13. end tri_test_id;  

14.   


  
  2)创建一个自定义过程 

Java代码 

1.  SQL>   create   or   replace   procedure   proc_test   as   

2.            begin   

3.            insert   into   test(cur_time)   values(sysdate);   

4.            end;   

5.            / 

6.    


  
  过程已创建。 
  
  3)创建JOB 

Java代码 

1.  SQL> declare job1 number; 

2.       begin 

3.          dbms_job.submit(job1,'proc_test;',sysdate,'sysdate+1/1440');--每天1440分钟,即一分钟运行test过程一次 

4.      end; 


  
  PL/SQL   JOB已成功完成。 

 

 

 

 

----------------------------------------------

 

 


--1 、建立一个存储过程,转历史并删除,假设表名名:test,历史表:test_his(两表结构一样):如

CREATE OR REPLACE PROCEDURE delhisdata AS

BEGIN

  INSERT INTO test_his

    SELECT * FROM test WHERE ins_date

  DELETE FROM test t WHERE ins_date

  COMMIT;

EXCEPTION

  WHEN OTHERS THEN

    ROLLBACK;

END;

/

--1、数据库中建立一个JOB对存储过程进行调用,并且每月执行一次,,

DECLARE

  jobno NUMBER;

BEGIN

  DBMS_JOB.SUBMIT(JOB       => jobno, /*自动生成JOB_ID*/

                  WHAT      => 'delhisdata;', /*需要执行的过程或SQL语句*/

                  NEXT_DATE => TRUNC(SYSDATE + 1) + 2 / 24, /*初次执行时间*/

                  INTERVAL  => 'TRUNC(add_months(SYSDATE,1))+2/24'); /*执行周期*/

  COMMIT;

END;

/

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool