Create backup tables periodically - oracle

I want to take a backup of two of my tables in Oracle Database. The backup should happen on each 15 days i.e. on 1st and 15th of each month.
Is there any way to automate this in Oracle through a procedure ?
Currently I do it may manually running a create table xxx_DDMM as select * from xxx;

Yes, you can schedule periodic jobs with Oracle Scheduler. Sample example:
--Create job.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'BACKUP_TABLE',
job_type => 'PLSQL_BLOCK',
job_action =>
q'[
begin
execute immediate 'create table xxx_'||to_char(sysdate, 'DDMM')||' as select * from xxx';
end;
]',
repeat_interval => 'FREQ=DAILY;BYMONTHDAY=1,15;BYHOUR=8;BYMINUTE=0;',
enabled => true);
END;
/
--Test and monitor job:
begin
dbms_scheduler.run_job('BACKUP_TABLE');
end;
/
select * from dba_scheduler_jobs where job_name = 'BACKUP_TABLE';
select * from dba_scheduler_job_run_details where job_name = 'BACKUP_TABLE' order by log_date;

Related

procedure scheduled job inside package

I have created a package purging inside that created procedure. How to schedule procedure around 2AM CST?
create or replace package body purging as
PROCEDURE del_exp IS
begin
FOR td IN (
SELECT
table_name
FROM
all_tables
where owner = 'ud_oi' and table_name not like 'lgh%'
)
LOOP
EXECUTE IMMEDIATE 'DELETE FROM ' || td.table_name || ' WHERE TRUNC(case_dt) < TRUNC(sysdate - 38)' ;
END LOOP;
end;
END del_exp;
A simple option is to use DBMS_JOB; if your database is recent, DBMS_SCHEDULER is preferred. You'd do it as follows:
DECLARE
X NUMBER;
BEGIN
SYS.DBMS_JOB.SUBMIT (
job => X,
what => 'begin purging.del_exp; end;',
next_date => TO_DATE ('16.06.2020 02:00:00', 'dd/mm/yyyy hh24:mi:ss'),
interval => 'TRUNC (SYSDATE+1) + 2 / 24',
no_parse => FALSE);
SYS.DBMS_OUTPUT.PUT_LINE ('Job Number is: ' || TO_CHAR (x));
COMMIT;
END;
/
A DBMS_SCHEDULER option (which also shows how to run it during working days (at least, working here where I live)):
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'purging',
job_type => 'PLSQL_BLOCK',
job_action => 'begin purging.del_exp; end;',
start_date => TO_TIMESTAMP_TZ ('16.06.2020 02:00 Europe/Zagreb',
'dd.mm.yyyy hh24:mi TZR'),
repeat_interval => 'FREQ=DAILY; BYDAY=MON,TUE,WED,THU,FRI; BYHOUR=2; BYMINUTE=0',
enabled => TRUE,
comments => 'Purge tables');
END;
/
Just to mention: condition you wrote:
table_name not like 'lgh%'
looks suspicious. Table names are - by default - in UPPERCASE. Unless you create tables using mixed (or lower) case, you can do that by enclosing their names into double quotes. I hope you didn't/don't do that. Therefore, you'd rather use
table_name not like 'LGH%'

DBMS_SCHEDULER in apex oracle

I have an API package written. It has the GET_INFO procedure. I want this procedure to be performed in the background every 20 minutes. I understand what I should do with dbms_scheduler. But in general I do not understand where to register them . I will be grateful for the example or for your help with this)
I wrote such a code but I don't know where to use it:
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name = 'My_Job',
job_type = 'STORED_PROCEDURE',
job_action = 'INSERT INTO TEST2(UPDATEDAT)
VALUES (sysdate);
END;',
start_date = 'systimestamp',
repeat_interval = 'FREQ=SECONDLY;INTERVAL=5',
end_date = null,
auto_drop = FALSE,
comments = 'My new job');
END;
Here is my code and I don't know where to store it.
As the job type implies, you need a procedure to be created such as
create or replace procedure Ins_Test2 is
begin
insert into Test2(updatedat) values(sysdate);
commit;
end;
and then create the scheduler through
begin
dbms_scheduler.create_job (
job_name => 'My_Job',
job_type => 'STORED_PROCEDURE',
job_action => 'Ins_Test2',
start_date => systimestamp,
repeat_interval => 'freq=minutely; interval = 20; byday=MON,TUE,WED,THU,FRI;',
enabled => true,
comments => 'My new job'
);
end;
where I added
byday=MON,TUE,WED,THU,FRI; as an extra direction if you want to run
the scheduler within the working days(you can omit that part if you'd
like).
systimestamp(get rid of quotes) for start_date might be
replaced with an upcoming time info such as start_date => '13-FEB-20 2.00.00PM Asia/Istanbul'
in my case.
And follow by listing the created schedulers by
select job_name, next_run_date
from dba_scheduler_jobs j;
And currently running ones by
select *
from user_scheduler_job_log l
order by l.log_date desc;
And drop the scheduler by
begin
dbms_scheduler.drop_job( job_name => 'My_Job' );
end;

Oracle Plsql : How to complete execution of a procedure without waiting for its subprogram to complete

In Oracle PlSql, I'm calling my stored procedure after performing some action on screen.
After execution of some piece of code I want to insert some records into a table but insert should happen after 20 mins. I want to run insert job/procedure independently so that my main procedure completes its execution.
Any sort of help would be appreciated !!
Hope this below snippet helps.
--Table to create dummy insertion
CREATE TABLE TEST_SO_TAB
(
COL1 NUMBER
);
--Procedure to replicate scenario
CREATE OR REPLACE
PROCEDURE TEST_SO_SCHEDULER(
p_in IN NUMBER)
AS
lv sys.odcinumberlist;
BEGIN
SELECT LEVEL BULK COLLECT INTO lv FROM dual CONNECT BY level < 100;
DBMS_SCHEDULER.create_job ( job_name => 'DELTA_REFRESH_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'DECLARE
lv1 sys.odcinumberlist;
BEGIN
SELECT LEVEL BULK COLLECT INTO lv1 FROM dual CONNECT BY level < 100;
IF lv1.EXISTS(1) THEN
FORALL i IN lv1.FIRST..lv1.LAST
INSERT INTO TEST_SO_TAB VALUES
( lv1(i)
);
END IF;
END',
start_date => SYSTIMESTAMP+ INTERVAL '20' MINUTE,
end_date => NULL,
enabled => TRUE,
comments => 'Job defined entirely by the CREATE JOB procedure.');
END;
/

How Can We Delete A Table Row Automatically After a Specific Time in PL SQL

I have Tow table with Identical Columns Given Below:
Table-1 PAYROLLFILE:
Table-2 TEMP_PAYROLLFILE:
I have written a PL SQL Function, which inserts a duplicate row from Table PAYROLLFILE to TEMP_PAYROLLFILE and remove the original row from Table PAYROLLFILE.So in short, Table TEMP_PAYROLLFILE is keeping a backup for deleted data from Table PAYROLLFILE.
Here is the PL SQL Function Code:
FUNCTION Remove_transaction_by_id(employee_id NUMBER)
RETURN CLOB
AS
cnt INT;
BEGIN
INSERT INTO temp_payrollfile
SELECT *
FROM payrollfile
WHERE empid = employee_id;
DELETE FROM payrollfile
WHERE empid = employee_id;
SELECT Count(*)
INTO cnt
FROM payrollfile;
COMMIT;
RETURN '<result><status>success</status> <row>'
||cnt
|| '</row></result>';
EXCEPTION
WHEN OTHERS THEN
RETURN '<result><status>Error</status></result>';
END remove_transaction_by_id;
However, I want to do more than that, I want to Delete Backup rows Automatically from Table TEMP_PAYROLLFILE after a specific time period is over fort that specific row.Do i need triggers.I am new on that and never done this type of work before.If anyone know this kind of technique, then i would really appreciate your help.please let me know if further details required.Thanks
Updates:
I have write this job to cleanup old rows: will it work?
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'oe.REMOVE_TEMP_PAYROLLFILE_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN DBMS_STATS.GATHER_TABLE_STATS('oe',
'TEMP_PAYROLLFILE');
delete from TEMP_PAYROLLFILE where RECORDDATE < sysdate-1;
END;',
start_date => '20-JAN-16 1.00.00AM US/Pacific',
repeat_interval => 'FREQ=DAILY',
end_date => '25-JAN-16 1.00.00AM US/Pacific',
enabled => TRUE,
comments => 'Gather table statistics');
END;
What you need here is a periodic cleanup job. Using DBMS_SCHEDULER you can configure a job that does this repeatedly:
delete from TEMP_PAYROLLFILE where recorddate < sysdate-60;
to delete records that are more than 60 days old.
These are some examples on how to create a scheduled job. As you see below, the job_action sections accepts a Pl/SQL script, in which you can place your cleanup logic.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'oe.my_job1',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN DBMS_STATS.GATHER_TABLE_STATS(''oe'',
''sales''); END;',
start_date => '15-JUL-08 1.00.00AM US/Pacific',
repeat_interval => 'FREQ=DAILY',
end_date => '15-SEP-08 1.00.00AM US/Pacific',
enabled => TRUE,
comments => 'Gather table statistics');
END;
/
Create a stored procedure to perform the deletion:
CREATE PROCEDURE remove_Temp_Transacts_by_ID(
in_employee_id IN NUMBER,
in_datetime IN TIMESTAMP
)
AS
BEGIN
DELETE FROM temp_payrollfile
WHERE empid = in_employee_id
AND datetime <= in_datetime;
END;
/
Then in your function include a call to schedule a job to run the procedure at a later time:
DBMS_SCHEDULER.CREATE_JOB(
JOB_NAME => 'Remove_TTbID__JOB',
JOB_TYPE => 'STORED_PROCEDURE',
JOB_ACTION => 'remove_Temp_Transacts_by_ID'
START_DATE => ( SYSTIMESTAMP + INTERVAL '2' HOURS ),
ENABLED => FALSE,
COMMENT => 'One time job to remove temporary payroll file entries'
);
DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
JOB_NAME => 'Remove_TTbID__JOB',
ARGUMENT_POSITION => 1,
ARGUMENT_VALUE => employee_id
);
DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
JOB_NAME => 'Remove_TTbID__JOB',
ARGUMENT_POSITION => 2,
ARGUMENT_VALUE => SYSTIMESTAMP
);
DBMS_SCHEDULER.ENABLE( 'Remove_TTbID__JOB' );

Scheduler event in Oracle

I have an event scheduler in mysql which is executing fine
CREATE event 'STORE' ON schedule every 1 hour doUPDATE c_store
SET is_active = '0'
WHERE web_id =
(
SELECT web_id
FROM c_web
WHERE Datediff(Now( ) , createdon ) > 15)
My query is i want the same in Oracle , can any one guide me.
You need to use DBMS_SCHEDULER to create a job and schedule it to run hourly.
For example,
SQL> BEGIN
2 DBMS_SCHEDULER.DROP_JOB (JOB_NAME => 'test_full_job_definition');
3 END;
4 /
PL/SQL procedure successfully completed.
SQL>
SQL> BEGIN
2 DBMS_SCHEDULER.create_job (
3 job_name => 'test_full_job_definition',
4 job_type => 'PLSQL_BLOCK',
5 job_action => 'BEGIN my_job_procedure; END;',
6 start_date => SYSTIMESTAMP,
7 repeat_interval => 'freq=hourly; byminute=0; bysecond=0;',
8 end_date => NULL,
9 enabled => TRUE,
10 comments => 'Job defined entirely by the CREATE JOB procedure.');
11 END;
12 /
PL/SQL procedure successfully completed.
SQL>
SQL> SELECT JOB_NAME, ENABLED FROM DBA_SCHEDULER_JOBS where job_name ='TEST_FULL_JOB_DEFINITION'
2 /
JOB_NAME ENABL
---------------------------------------- -----
TEST_FULL_JOB_DEFINITION TRUE
SQL>
See more examples here
You must use DBMS_SCHEDULER
Here you can find some examples.

Resources