Oracle日志定期清理存储过程
常要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;
/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

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

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.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

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())

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.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

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.
