Home Database Mysql Tutorial Oracle日志定期清理存储过程

Oracle日志定期清理存储过程

Jun 07, 2016 pm 05:00 PM
database

常要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 of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

See all articles