Oracle job to run daily in Oracle Express edition 11g - oracle

![enter image description here][1]
I just updated with a new version of the database. I've previously used Oracle database versioyon 10. Now I installed Oracle 11g Express edition.
I had the three jobs on the DBMS_jobs.
I programmed it to run them at a particular time on a daily basis. every day at 01:00 in the stored procedure was running on a regular basis.
begin
sys.dbms_job.submit(job => :job,
what => 'BEGIN CALL_MY_SP; END;',
next_date => to_date('24-09-2013 01:00:00', 'dd-mm-yyyy hh24:mi:ss'),
interval => 'TRUNC(SYSDATE + 1) + 1/24');
commit;
end;
Although the same job to the new Oracle database running. But this job is manually executing.
How do I run automatically on a regular basis every day? But What if I did not!
http://i.stack.imgur.com/rePVq.png
your job
http://l1309.hizliresim.com/1f/s/sz6pg.png

To run manually, try:
exec dbms_scheduler.run_job('MY_JOB',false);
and to stop manually, use:
exec dbms_scheduler.stop_job('MY_JOB',false);
If your question is how to scheduler a job in Oracle, its a big topic (see Documentation ), but a simple example would be:
BEGIN
dbms_scheduler.create_job(
job_name=>'JOB_TEST2',
job_type=>'PLSQL_BLOCK',
job_action=>'BEGIN
dbms_lock.sleep(10);
END;',
start_date=>systimestamp,
repeat_interval=>'FREQ=MINUTELY;INTERVAL=1',
number_of_arguments=>0,
enabled=>TRUE,
comments=>'Job duration is 10 seconds, launches every minute'
);
END;
Since it was created as enabled, it will start running automatically. You can verify this looking at the logs (dba_scheduler_job_run_details) and can view basic attributes via dba_scheduler_jobs

Related

How to Automate Oracle SQL Developer to export a query to csv/xlsx once a day

I'm fairly new to SQL and my team has been tasked with running a daily report which requires SQL data. Is there a way to use Oracle SQL Developer to automatically export a query to csv/excel on a daily basis?
The program Oracle SQL Developer itself won't automate it, but there are plenty of options to get it done.
Basically, some computer somewhere needs to have a scheduler that can wake it up and do the work. Some people use their own computers for this, using windows scheduled tasks, cron jobs, or 3rd party programs.
But what "tasks" will your computer run? The task is basically to connect to the database, authenticate, send the Query text, retrieve the results in memory, and then export them as a file. Lot's of people will use python for this, because it can handle all of those steps.
You'll notice that we're already at 2 external "things" that could fail.
(1) your computer,
(2) your python code.
A simpler option, is the oracle database itself. Is it on a remote server that's always running? If so, you might take advantage of that and have it do all the work.
You're looking at a few steps, but I think they are easier (you might have to get permissions from an admin, though).
Specify a file location - remember, this is on the server, not your computer
create or replace directory csv_dir as '/destination/for/results';
Create a stored procedure - acts like a container for your SQL logic.
create or replace procedure write_file is
file_handle UTL_FILE.file_type;
begin
file_handle := utl_file.fopen('CSV_DIR', 'csv_filename.csv', 'w', 32767);
for rws in (
select * from t -- your query here
) loop
utl_file.put_line(file_handle,
rws.c1 || ',' || rws.c2 || ',' || rws.c3 -- your columns here
);
end loop;
utl_file.fclose(file_handle);
end write_file;
Create a scheduler job - runs the stored procedure. This is a feature of your Oracle server.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'EXPORT_CSV_J',
job_type => 'PLSQL_BLOCK',
job_action => 'begin write_file; end;',
number_of_arguments => 0,
start_date => NULL,
repeat_interval => 'FREQ=DAILY',
end_date => NULL,
enabled => FALSE,
auto_drop => FALSE);
DBMS_SCHEDULER.SET_ATTRIBUTE(
name => 'EXPORT_CSV_J',
attribute => 'logging_level',
value => DBMS_SCHEDULER.LOGGING_RUNS);
DBMS_SCHEDULER.enable(
name => 'EXPORT_CSV_J');
END;
I borrowed this code from this website since it has been a while since I did this myself.
If for some reason your database isn't actually Oracle (I know some people use Oracle SQL Developer even though their actual database is something different) then the steps will be similar but the code will be different.

How to schedule a series of stored procedures to run sequentially?

I am new to coding in SQl and new to Oracle SQL developer as well.
I managed to re-build an old MS Access database in Oracle . I have stored they queries in procedures but now I need to run the procedures in a specific order, a bit like you would do in a MS Access Macro.
The goal is to be able to create a daily routine that should be launched by the scheduler.
is there a way to accomplish this?
ie "macro full run"
run procedure a and when it is finished
run procedure b and when it is finished
run procedure c and so on....
thank you in advance
Valentina
You can create a new procedure
CREATE OR REPLACE PROCEDURE MACRO_FULL_RUN AS
BEGIN
PROCEDURE_A;
PROCEDURE_B;
PROCEDURE_C;
... and so on
END;
And then put it in the scheduler:
BEGIN
DBMS_SCHEDULER.Create_job (
job_name => 'MY_JOB_THAT_RUNS_DAILY',
repeat_interval => 'FREQ=DAILY',
job_type => 'STORED_PROCEDURE',
job_action => 'MACRO_FULL_RUN',
enabled => TRUE
);
END;

where do i find dbms_scheduler in PL/SQL?

I have created a dbms_scheduler job in sql developer, but in PL/SQL Developer i cannot see this job. I get see only the dbms_job.
Have some way to see the dbms_scheduler job in UI in PL/SQL Developer?
You can find DBMS_SCHEDULER jobs in the Jobs folder in recent versions of PL/SQL Developer. In older versions you'll have to run select * from all_scheduler_jobs;
Your old version was probably created before DBMS_SCHEDULER existed, so your Jobs folder only reads from ALL_JOBS. Newer versions have two folders, DBMS_Jobs that reads from ALL_JOBS, and Jobs that reads from ALL_SCHEDULER_JOBS. When you find your scheduler job you can right-click on it and either view or edit the properties.
For example, if I create this job:
begin
dbms_scheduler.create_job
(
job_name => 'TEST_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'begin null; end;',
auto_drop => false);
end;
/
I see these results in the Object window:

Oracle Scheduler – Creating a complex schedule with a single job

I have a Job running on an Oracle 10g DB with a pretty simple execution plan.
BYDAY=MON,TUE,WED,THU,FRI;BYHOUR=6,10,14,18
Now the problem is that we have to change the plan – but just for Mondays. So on Mondays the first job should run at 8 instead of 6. Then like all the others (10, 14, 18).
Whereas from Tuesday to Friday it should run as above.
The easiest way would be to create a second job for the Monday and to remove the Monday from the original job.
This, however, is a problem for our application, as it relies on just one job per import.
So the question is: Is there a way to tell the scheduler to run at 6,10,14,18 o'clock TUE-FRI and 8,10,14,18 on MON with a single job?
I read about specifying a repeat interval with a PL/SQL expression. But I didn't find out if there is way to do it.
Thanks for your help
create a composite schedule, and assign that to the job when you create it.
for example:
begin
dbms_scheduler.create_schedule('MY_SCHED1', repeat_interval =>
'FREQ=DAILY;BYDAY=TUE,WED,THU,FRI;BYHOUR=6,10,14,18');
END;
/
begin
dbms_scheduler.create_schedule('MY_SCHED2', repeat_interval =>
'FREQ=DAILY;BYDAY=MON;BYHOUR=8,10,14,18');
END;
/
begin
dbms_scheduler.create_schedule('MAIN', repeat_interval =>
'MY_SCHED1, MY_SCHED2');
END;
/
now you'd just assign the "MAIN" named schedule to the job instead of the schedule string.

newbie on using Oracle Scheduler to start a job based on event

I have a table in Oracle 11.2 database. I want the database to run an executable file on a remote server if a specific cell in table1 is updated to a value of 1 AND if the number of existing rows in table2 is > 0. I don't have much experience with what is possible in databases -- is the following possible to achieve this?
create a job using Oracle Scheduler. The job runs immediately, and is used to run an external executable program on a remote server. The job exists, but is not run until step 5 below (is this possible?).
http://docs.oracle.com/cd/E11882_01/server.112/e17120/schedadmin001.htm#BAJHIDDC
attach a DML trigger to the column of the table that fires on an UPDATE statement.
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#CIHEHBEB
have the trigger invoke a PL/SQL subprogram
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#CIHEGACF
in the PL/SQL subprogram, perform the following business logic: if a specific cell in table1 equals 1, AND if the number of rows in table 2 is greater than 0, proceed to step 5, otherwise stop (exit, quit).
Run the job in step 1
Or, if the job/scheduler is not made to provide this functionality, is there another way to achieve the same thing? That is, have a change in a database table trigger an external job.
UPDATE 1:
I wonder if it's possible to implement steps 1-5 above by just using Oracle Scheduler with DBMS_SCHEDULER.CREATE_JOB using parameter event_condition?
http://docs.oracle.com/cd/E11882_01/server.112/e25494/scheduse005.htm#CHDIAJEB
Here's an example from the above link:
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'process_lowinv_j1',
program_name => 'process_lowinv_p1',
event_condition => 'tab.user_data.event_type = ''LOW_INVENTORY''',
queue_spec => 'inv_events_q, inv_agent1',
enabled => TRUE,
comments => 'Start an inventory replenishment job');
END;
The above code creates a job that starts when an application signals the Scheduler that inventory levels for an item have fallen to a low threshold level.
Could the above code somehow be modified to perform the intended steps? For example, could steps 2-4 above be eliminated by using event_condition here instead? etc. If so, what would it look like, for example, how to set the queue_spec?
Assuming that you install the Oracle Scheduler Agent on the remote server, DBMS_SCHEDULER can run an executable on the remote machine. I would have the DML trigger enqueue a message into an Oracle Advanced Queue (AQ) and use that queue to create an event-based job (DML triggers are not allowed to commit or rollback the transaction but running a DBMS_SCHEDULER job implicitly issues a commit so DML triggers cannot directly run a job). The event-based job and the job that runs the remote executable would be part of a job chain.

Resources