How to alter oracle job to run every 12hrs - oracle

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>

Related

how to run an exe file with given parameters in pl\sql oracle db

i am transfroming oracle forms codes to oracle stored procedure so i need your helps.
can anybody tell me how to run an exe file with parameters in plsql code? i have tried many ways to do it. you can see sample code of it below.
here is my code
DECLARE
v_onay NUMBER;
CURSOR c1 IS
SELECT id
FROM t1
WHERE tarih = :main.tarih
AND creditid IN
(SELECT kredid FROM t2 WHERE tarih = :main.tarih)
ORDER BY id;
AppID PLS_INTEGER;
BEGIN
v_onay := sor('GİRİLEN TARİH İÇİN GEÇ YAPILAN ÖDEMELERİ TEKRAR ÇALIŞTIRMAK İSTİYOR MUSUNUZ? ');
IF v_onay = 1 THEN
entegre.Krdtfrs9_Doldur(1, :main.tarih);
mess('Kayıtlar güncellendi. OUTPUT verileri hazırlanacak', 0);
FOR r1 in c1 LOOP
COMMIT;
AppID := DDE.App_Begin('C:\Muhasebe\tfrs\Krd\ID\TFRS9KRD_ID.exe ' ||
TO_CHAR(:main.tarih, 'DD.MM.YYYY') || ' ' ||r1.id,
DDE.App_Mode_Maximized);
END LOOP;
END IF;
mess('Güncelleme tamamlandı', 0);
END;
thanks a lot.
As far as I can tell, if you want to run an operating system executable file from PL/SQL stored procedure, you'd schedule it using the DBMS_SCHEDULER built-in package.
Something like this:
You'd pass tarih and id as procedure's parameters.
SQL> CREATE OR REPLACE PROCEDURE p_exe (par_tarih DATE, par_id NUMBER)
2 AS
3 l_name VARCHAR (20) := 'GUNES';
4 l_tarih VARCHAR2 (10) := TO_CHAR (par_tarih, 'dd.mm.yyyy');
5 BEGIN
6 -- drop job if it already exists
7 BEGIN
8 DBMS_SCHEDULER.drop_job (l_name);
9 EXCEPTION
10 WHEN OTHERS
11 THEN
12 NULL;
13 END;
14
15 -- create a new job
16 DBMS_SCHEDULER.create_job (
17 job_name => l_name,
18 job_type => 'EXECUTABLE',
19 job_action => 'C:\Muhasebe\tfrs\Krd\ID\TFRS9KRD_ID.exe',
20 number_of_arguments => 2,
21 enabled => FALSE);
22
23 -- set arguments
24 DBMS_SCHEDULER.set_job_argument_value (job_name => l_name,
25 argument_position => 1,
26 argument_value => par_tarih);
27 DBMS_SCHEDULER.set_job_argument_value (job_name => l_name,
28 argument_position => 2,
29 argument_value => par_id);
30
31 -- enable job
32 DBMS_SCHEDULER.enable (l_name);
33 END;
34 /
Procedure created.
SQL>

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%'

How to rename scheduler job Oracle

I would like rename scheduler job in Oracle, is it possible?
dba_scheduler_jobs(owner = "db", name = "my_job")
=> dba_scheduler_jobs(owner = "db", name = "my_own_job");
Thanks for answers.
A job is a database object, so the RENAME command works natively
SQL> begin
2 dbms_scheduler.create_job (
3 job_name => 'MY_BAD_NAME',
4 job_type => 'PLSQL_BLOCK',
5 job_action => 'begin null; end;',
6 start_date => systimestamp,
7 repeat_interval => 'freq=hourly; byminute=0; bysecond=0;',
8 enabled => true);
9 end;
10 /
PL/SQL procedure successfully completed.
SQL> rename MY_BAD_NAME to BETTER_NAME;
Table renamed.
SQL> select job_name from user_scheduler_jobs;
JOB_NAME
--------------------------------------------------------------------------------
BETTER_NAME
Instead of rename, you can create/replicate job and drop the old one.
Below, my_new_job1 has created.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'my_new_job1',
program_name => 'my_saved_program',
repeat_interval => 'FREQ=DAILY;BYHOUR=12',
comments => 'Daily at noon');
END;
/
Below, my_new_job2 has created with same details as my_new_job1
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'my_new_job2',
program_name => 'my_saved_program',
repeat_interval => 'FREQ=DAILY;BYHOUR=12',
comments => 'Daily at noon');
END;
/
Below an old job my_new_job1 has been deleted.
BEGIN
DBMS_SCHEDULER.DROP_JOB ('my_new_job1');
END;
/

Oracle Scheduler proc doesn't complete before next start time

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

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