I want to know what will happen if a scheduler takes more than hour to complete but the scheduler is configured to re-run for every hour. Would it create multiple running sessions or would it wait for the previous run to complete?
It will wait. I'll start this job every minute, but it takes 2 mins to run
SQL> create table t ( tag varchar2(10), d date);
Table created.
SQL>
SQL> create or replace
2 procedure P is
3 begin
4 insert into t values ('start',sysdate);
5 commit;
6 dbms_lock.sleep(120);
7 insert into t values ('end',sysdate);
8 commit;
9 end;
10 /
Procedure created.
SQL>
SQL> begin
2 dbms_scheduler.create_job (
3 job_name => 'JOB1',
4 job_type => 'PLSQL_BLOCK',
5 job_action => 'begin p; end;',
6 start_date => systimestamp,
7 repeat_interval => 'freq=minutely;bysecond=0;',
8 enabled => true);
9 end;
10 /
PL/SQL procedure successfully completed.
SQL> select * from t;
TAG D
---------- -------------------
start 03/05/2018 22:47:00
end 03/05/2018 22:49:00
start 03/05/2018 22:49:00
Related
We have a requirement to disable a trigger only for a specific procedure run in ORACLE.
We have Table A and Table B.
The trigger is designed to insert into Table B for each insert or update of Table A.
The procedure makes entries to Table A.
How to enable/disable trigger in procedure?
The above link explains how to enable and disable the trigger during the procedure.
But my requirement is during this procedure run, if there are other inserts to Table A, then the trigger should run as expected.
Is this possible? Please help on how to achieve the same if possible.
You can use the DBMS_APPLICATION_INFO and SYS_CONTEXT to set any parameter in your procedure and then check for that parameter in the TRIGGER to see if you should take any action in the trigger or not as follows:
Table description:
SQL> DESC AA;
Name Null? Type
----------------------------------------- -------- ----------------------------
COL1 NOT NULL NUMBER
SQL>
Trigger code:
SQL> CREATE OR REPLACE TRIGGER AA_TRG BEFORE
2 INSERT OR UPDATE ON AA
3 FOR EACH ROW
4 BEGIN
5 IF SYS_CONTEXT(
6 'USERENV',
7 'ACTION'
8 ) = 'DISABLE_TRIGGER' THEN
9 DBMS_OUTPUT.PUT_LINE('TRIGGER DISABLED');
10 ELSE
11 DBMS_OUTPUT.PUT_LINE('TRIGGER ENABLED');
12 END IF;
13
14 DBMS_APPLICATION_INFO.SET_ACTION(ACTION_NAME => NULL);
15 END;
16 /
Trigger created.
SQL>
Disabled trigger procedure code:
SQL> CREATE OR REPLACE PROCEDURE AA_DISABLED_TRIGGER_PROC AS
2 BEGIN
3 DBMS_APPLICATION_INFO.SET_ACTION(ACTION_NAME => 'DISABLE_TRIGGER');
4 INSERT INTO AA VALUES ( 3 );
5
6 ROLLBACK;
7 END;
8 /
Procedure created.
SQL>
Enabled trigger procedure code:
SQL> CREATE OR REPLACE PROCEDURE AA_ENABLED_TRIGGER_PROC AS
2 BEGIN
3 INSERT INTO AA VALUES ( 3 );
4
5 ROLLBACK;
6 END;
7 /
Procedure created.
SQL>
Calling the procedures:
SQL>
SQL> SET SERVEROUT ON
SQL> BEGIN
2 AA_ENABLED_TRIGGER_PROC;
3 END;
4 /
TRIGGER ENABLED
PL/SQL procedure successfully completed.
SQL>
SQL> SET SERVEROUT ON
SQL> BEGIN
2 AA_DISABLED_TRIGGER_PROC;
3 END;
4 /
TRIGGER DISABLED
PL/SQL procedure successfully completed.
SQL>
SQL> SET SERVEROUT ON
SQL> BEGIN
2 AA_ENABLED_TRIGGER_PROC;
3 END;
4 /
TRIGGER ENABLED
PL/SQL procedure successfully completed.
SQL>
Not possible. If trigger is disabled, then it is disabled and won't work.
But, if you alter the table and add a column which says who is inserting rows into it, then you could use trigger's WHEN clause to distinguish this procedure from other inserters and either fire the trigger or not. It means, of course, that trigger remains enabled all the time.
I need below job to be run at every 12 hrs (example : job need to be run daily at 12:30am & 12:30pm) please someone help
Code :
BEGIN
SYS.DBMS_JOB.CHANGE
(
job => 123
,what => 'SP_ABC;'
,next_date => TO_DATE('08/09/2020 00:30:00','dd/mm/yyyy hh24:mi:ss')
,INTERVAL => 'TRUNC(sysdate+1)+30/1440'
);
END;
/
COMMIT;
That would be something like this:
SQL> set serveroutput on
SQL> declare
2 l_job number;
3 begin
4 dbms_job.submit
5 (job => l_job,
6 what => 'p_test;',
7 next_date => trunc(sysdate + 1) + 0/24 + 30/(24*60),
8 interval => 'trunc(sysdate + 1) + 12/24 + 30/(24*60)'
9 );
10 commit;
11 dbms_output.put_line('job number = ' || l_job);
12 end;
13 /
job number = 151
PL/SQL procedure successfully completed.
SQL> select job, last_date, next_date from user_jobs;
JOB LAST_DATE NEXT_DATE
---------- ------------------- -------------------
151 08.09.2020 00:30:00
SQL>
Or, if you switch to DBMS_SCHEDULER:
SQL> begin
2 sys.dbms_scheduler.create_job
3 (job_name => 'test',
4 job_type => 'plsql_block',
5 job_action => 'begin p_test; end;',
6 start_date => sysdate,
7 repeat_interval => 'freq=daily; byhour=0,12; byminute=30',
8 enabled => true
9 );
10 end;
11 /
PL/SQL procedure successfully completed.
SQL> select repeat_interval, next_run_date from user_scheduler_jobs;
REPEAT_INTERVAL NEXT_RUN_DATE
---------------------------------------- ----------------------------------------
freq=daily; byhour=0,12; byminute=30 08.09.20 00:30:05,000000 +02:00
SQL>
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;
I want to RUN SP2 inside SP1. The SP2 inserts several data into a table, one of those data is actual date.
So, i want that SP2 only execute if in the column date, the date is within the last 10 days.
It´s possible?
Thanks
Here's an example.
First, create a table and insert some sample rows:
SQL> create table my_tbl
2 (id number,
3 my_col date
4 );
Table created.
SQL> insert into my_tbl (id, my_col)
2 -- more than 10 days ago
3 select 1, date '2018-05-25' from dual union all
4 -- less than 10 days ago
5 select 2, date '2018-09-01' from dual;
2 rows created.
Create two procedures; SP_1 is supposed to call SP_2 if condition is met. I'm looping through both rows in the table; one row's date value is OK, another one isn't.
SQL> create or replace procedure sp_2 as
2 begin
3 dbms_output.put_line('...I am in SP_2 now');
4 end;
5 /
Procedure created.
SQL> create or replace procedure sp_1 as
2 begin
3 for cur_r in (select my_col from my_tbl order by id) loop
4 dbms_output.put_line('date value = ' || to_char(cur_r.my_col, 'yyyy-mm-dd'));
5 if cur_r.my_col >= trunc(sysdate) - 10 then
6 dbms_output.put_line('...calling SP_2');
7 sp_2;
8 else
9 dbms_output.put_line('...date condition failed - not calling SP_2');
10 end if;
11 end loop;
12 end;
13 /
Procedure created.
Testing:
SQL> set serveroutput on
SQL> begin
2 sp_1;
3 end;
4 /
date value = 2018-05-25
...date condition failed - not calling SP_2
date value = 2018-09-01
...calling SP_2
...I am in SP_2 now
PL/SQL procedure successfully completed.
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.