应用Oracle job和存储过程
每月新增数据百万多条,需要定期处理2个主要数据表(test_ad,test_pd),移动非当月数据到历史表中保存数据操作存储过程如下:MYPR
每月新增数据百万多条,需要定期处理2个主要数据表(test_ad,test_pd),移动非当月数据到历史表中保存
数据操作存储过程如下:
MYPROC.prc
create or replace procedure MYPROC is
TableName_AD char(13);
TableName_PD char(13);
tmp_str varchar2(100);
tmp_str2 varchar2(100);
tmp_str3 varchar2(100);
tmp_str4 varchar2(100);
tmp_str5 varchar2(100);
tmp_str6 varchar2(100);
tmp_str7 varchar2(100);
tmp_str8 varchar2(100);
begin
--临时表名赋值
if TableName_AD is null then
select 'PA_AD_'||to_char(add_months(sysdate,-1),'yyyymm') into TableName_AD from dual;
end if;
if TableName_PD is null then
select 'PA_PD_'||to_char(add_months(sysdate,-1),'yyyymm') into TableName_PD from dual;
end if;
--创建(test_ad)历史表
tmp_str:='create table '||TableName_AD||' as select * from test_ad where patroldateexecute immediate tmp_str;
--创建(test_pd)历史表
tmp_str2:='create table '||TableName_PD||' as select * from test_pd where patroldateexecute immediate tmp_str2;
--commit;
--创建本月数据临时表
tmp_str3:='create table temp_ad as select * from test_ad Where patroldate > ='''||to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd')||'''';
execute immediate tmp_str3;
tmp_str4:='create table temp_pd as select * from test_pd Where patroldate > ='''||to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd')||'''';
execute immediate tmp_str4;
--删除本月数据表(test_ad,test_pd)
tmp_str5:='drop TABLE test_ad';
execute immediate tmp_str5;
tmp_str6:='drop TABLE test_pd';
execute immediate tmp_str6;
--本月数据临时表重命名表(test_ad,test_pd)
tmp_str7:='rename temp_ad to test_ad';
tmp_str8:='rename temp_pd to test_pd';
execute immediate tmp_str7;
execute immediate tmp_str8;
/*--删除主数据表上非本月记录 --删除历史表本月记录 --插入操作记录 --=============注意!在存储过程中使用CREATE或DROP需要显示授权========== 注:上面有一段被注释的内容,是最初的方案,但是后来了解到:删除大量数据,Oracle并不释放空间!所以用了现在的方案,复制创建表——>删除表——>重命名。 以上存储过程每月3号定期执行 declare job1 number; 为了补救意外导致3号午夜12点没有执行MYPROC 使用另外一个存储过程验证MYPROC是否执行 PASUPPLYPROC.prc create or replace procedure PASUPPLYPROC is --=============注意!在存储过程中使用CREATE或DROP需要显示授权========== 第二个job定期执行PASUPPLYPROC验证 declare job1 number; DBA_JOBS
delete from test_ad where patroldate
delete from test_pd where patroldate
tmp_str2:='delete from '||tmp_TableName||' where patroldate>='||to_date(to_char(add_months(last_day(sysdate) +1,-1),'yyyymmdd'),'yyyymmdd');
execute immediate tmp_str2;
*/
insert into oper_proc_log values(sysdate,'pc',0);
commit;
end MYPROC;
--grant create table to user,grant drop any table to user
/
begin
--每月3号午夜12点执行MYPROC
dbms_job.submit(job1,'MYPROC;',sysdate,'TRUNC(LAST_DAY(SYSDATE ) + 3)');
commit;
end;
isnull integer;
tmp_str varchar2(100);
begin
--取得本月执行myproc次数
if isnull is null then
select count(*) into isnull from oper_proc_log
where oper_date>=to_date(to_char(add_months(last_day(sysdate) +1,-1), 'yyyymmdd'),'yyyymmdd');
end if;
--无本月执行记录则立即执行myproc
if isnull=0 then
tmp_str:='begin myproc; end;';
execute immediate tmp_str;
commit;
end if;
end PASUPPLYPROC;
--grant create table to user,grant drop table to user
/
begin
--每月15号午夜12点10分执行PASUPPLYPROC
dbms_job.submit(job1,'PASUPPLYPROC;',sysdate,'TRUNC(LAST_DAY(SYSDATE) + 14) +(24*60+10)/(24*60)');
commit;
end;
===========================================
字段(列) 类型 描述
JOB NUMBER 任务的唯一标示号
LOG_USER VARCHAR2(30) 提交任务的用户
PRIV_USER VARCHAR2(30) 赋予任务权限的用户
SCHEMA_USER VARCHAR2(30) 对任务作语法分析的用户模式
LAST_DATE DATE 最后一次成功运行任务的时间
LAST_SEC VARCHAR2(8) 如HH24:MM:SS格式的last_date日期的小时,分钟和秒
THIS_DATE DATE 正在运行任务的开始时间,如果没有运行任务则为null
THIS_SEC VARCHAR2(8) 如HH24:MM:SS格式的this_date日期的小时,分钟和秒
NEXT_DATE DATE 下一次定时运行任务的时间
NEXT_SEC VARCHAR2(8) 如HH24:MM:SS格式的next_date日期的小时,分钟和秒
TOTAL_TIME NUMBER 该任务运行所需要的总时间,单位为秒
BROKEN VARCHAR2(1) 标志参数,Y标示任务中断,以后不会运行
INTERVAL VARCHAR2(200) 用于计算下一运行时间的表达式
FAILURES NUMBER 任务运行连续没有成功的次数
WHAT VARCHAR2(2000) 执行任务的PL/SQL块
CURRENT_SESSION_LABEL RAW MLSLABEL 该任务的信任Oracle会话符
CLEARANCE_HI RAW MLSLABEL 该任务可信任的Oracle最大间隙
CLEARANCE_LO RAW MLSLABEL 该任务可信任的Oracle最小间隙
NLS_ENV VARCHAR2(2000) 任务运行的NLS会话设置
MISC_ENV RAW(32) 任务运行的其他一些会话参数
描述 INTERVAL参数值
每天午夜12点 'TRUNC(SYSDATE + 1)'
每天早上8点30分 'TRUNC(SYSDATE + 1) + (8*60+30)/(24*60)'
每星期二中午12点 'NEXT_DAY(TRUNC(SYSDATE ), ''TUESDAY'' ) + 12/24'
每个月第一天的午夜12点 'TRUNC(LAST_DAY(SYSDATE ) + 1)'
每个季度最后一天的晚上11点 'TRUNC(ADD_MONTHS(SYSDATE + 2/24, 3 ), 'Q' ) -1/24'
每星期六和日早上6点10分 'TRUNC(LEAST(NEXT_DAY(SYSDATE, ''SATURDAY"), NEXT_DAY(SYSDATE, "SUNDAY"))) + (6×60+10)/(24×60)'

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

AI Hentai Generator
Generate AI Hentai for free.

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.

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.

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 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.

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.

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.
