Apply Oraclejob and stored procedures

Source: Internet
Author: User
There are more than one million new data records each month, and two major data tables (test_ad, test_pd) need to be processed on a regular basis. The Stored Procedure for moving non-existing data to the historical table is as follows: MYPR

There are more than one million new data records each month, and two major data tables (test_ad, test_pd) need to be processed on a regular basis. The Stored Procedure for moving non-existing data to the historical table is as follows: MYPR

More than one million new data records are added each month, and two major data tables (test_ad, test_pd) need to be processed on a regular basis to move data other than the current month to the historical table for storage

The data operation stored procedure is as follows:

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
-- Temporary table name assignment
If TableName_AD is null then
Select 'Pa _ AD _ '| to_char (add_months (sysdate,-1), 'yyymmm') into TableName_AD from dual;
End if;
If TableName_PD is null then
Select 'Pa _ PD _ '| to_char (add_months (sysdate,-1), 'yyymmm') into TableName_PD from dual;
End if;

-- Create a (test_ad) History Table
Tmp_str: = 'create table' | TableName_AD | 'as select * from test_ad where patroldate <''' | to_date (to_char (add_months (last_day (sysdate) + 1, -1), 'yyyy-mm-dd'), 'yyyy-mm-dd') | '''';
Execute immediate tmp_str;

-- Create a (test_pd) History Table
Tmp_str2: = 'create table' | TableName_PD | 'as select * from test_pd where patroldate <''' | to_date (to_char (add_months (last_day (sysdate) + 1, -1), 'yyyy-mm-dd'), 'yyyy-mm-dd') | '''';
Execute immediate tmp_str2;
-- Commit;

-- Create a temporary table for this month
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;

-- Delete this month's data table (test_ad, test_pd)
Tmp_str5: = 'drop TABLE test_ad ';
Execute immediate tmp_str5;
Tmp_str6: = 'drop TABLE test_pd ';
Execute immediate tmp_str6;
-- Rename the temporary table for this month (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;

/* -- Delete non-monthly records from the master data table
Delete from test_ad where patroldate -- Delete the month record of a history table
Delete from test_pd where patroldate

-- Delete the month record of a history table
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 operation records
Insert into oper_proc_log values (sysdate, 'pc', 0 );
Commit;
End MYPROC;

-- ================ Note! To use CREATE or DROP in a stored procedure, you need to display authorization ==========
-- Grant create table to user, grant drop any table to user
/

Note: There is a comment on the content above, which is the original solution. But later I learned that Oracle does not release space When deleting a large amount of data! So we use the current solution to copy and create a table --> delete a table --> rename it.

The above stored procedures are regularly executed on the 3rd of each month.

Declare job1 number;
Begin
-- Execute MYPROC at midnight on the third day of every month
Dbms_job.submit (job1, 'myproc; ', sysdate, 'trunc (LAST_DAY (SYSDATE) + 3 )');
Commit;
End;

To remedy the problem, MYPROC was not executed at midnight on the 3rd.

Verify that MYPROC is executed using another Stored Procedure

PASUPPLYPROC. prc

Create or replace procedure PASUPPLYPROC is
Isnull integer;
Tmp_str varchar2 (100 );
Begin
-- Obtain the number of times myproc is executed this month
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;
-- If the execution record for this month does not exist, execute myproc immediately.
If isnull = 0 then
Tmp_str: = 'in in myproc; end ;';
Execute immediate tmp_str;
Commit;
End if;
End PASUPPLYPROC;

-- ================ Note! To use CREATE or DROP in a stored procedure, you need to display authorization ==========
-- Grant create table to user, grant drop table to user
/

The second job regularly performs PASUPPLYPROC verification.

Declare job1 number;
Begin
-- Execute PASUPPLYPROC at on the 15th of every month.
Dbms_job.submit (job1, 'pasupplyproc; ', sysdate, 'trunc (LAST_DAY (SYSDATE) + 14) + (24*60 + 10)/(24*60 )');
Commit;
End;

DBA_JOBS
========================================================== ===
Field (column) type description
Unique ID of a JOB NUMBER
LOG_USER VARCHAR2 (30) user who submits the task
PRIV_USER VARCHAR2 (30) user with task Permissions
SCHEMA_USER VARCHAR2 (30) User Mode for Syntactic Analysis of tasks
LAST_DATE DATE the time when the last task was successfully run
LAST_SEC VARCHAR2 (8) such as HH24: MM: hour, minute, and second of the SS format last_date
The start time of the task being run by THIS_DATE. If no task is running, the value is null.
THIS_SEC VARCHAR2 (8) such as HH24: MM: the hour, minute, and second of the this_date date in SS format
NEXT_DATE DATE the next scheduled task running time
NEXT_SEC VARCHAR2 (8) such as HH24: MM: the hour, minute, and second of the SS format next_date
TOTAL_TIME NUMBER the total time required to run the task, in seconds
The BROKEN VARCHAR2 (1) Flag parameter. Y indicates that the task is interrupted and will not run later.
INTERVAL VARCHAR2 (200) is the expression used to calculate the next line time.
Failures number the NUMBER of times the task failed to run continuously
WHAT VARCHAR2 (2000) PL/SQL block for task execution
CURRENT_SESSION_LABEL raw mlslabel trust Oracle session character of the task
CLEARANCE_HI raw mlslabel maximum gap between Oracle databases trusted by this task
CLEARANCE_LO raw mlslabel minimum Oracle gap trusted by this task
NLS session settings for running the NLS_ENV VARCHAR2 (2000) task
Other session parameters of the MISC_ENV RAW (32) task


Parameter Value of INTERVAL
'Trunc (SYSDATE + 1) 'At midnight every day )'
Every morning 08:30 'trunc (SYSDATE + 1) + (8*60 + 30)/(24*60 )'
NEXT _ DAY (TRUNC (SYSDATE), ''tuesday'') + 12/24 at every TUESDAY'
'Trunc (LAST_DAY (SYSDATE) + 1) 'at midnight on the first day of each month )'
'Trunc (ADD_MONTHS (SYSDATE + 2/24, 3), 'q')-100' at on the last day of each quarter'
'Trunc (LEAST (NEXT_DAY (SYSDATE, ''saturday "), NEXT_DAY (SYSDATE," SUNDAY ") + (6 × 60 + 10) /(24 × 60 )'

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.