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

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;

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.

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:

PLSQL DBMS_SCHEDULER wait for response

I've created a DBMS_SCHEDULER to run a batch-script from PL/SQL.
The script starts as expected but now I need to find a way to pause my PL/SQL code until the batch-script has done it's work. My batch does something like
Read csv-files of amount x
Copy the csv-values into an existing Excel-file
Zip those files.
After that my PL/SQL code should start again. The batch-process can take from few seconds up to one hour depends on the amount of values. Is there a way how I can pause my script and wait for a response?
Here my code
BEGIN
DBMS_SCHEDULER.create_job ('myjob',
job_action => 'C:\WINDOWS\SYSTEM32\CMD.EXE',
number_of_arguments => 3,
job_type => 'executable',
enabled => FALSE);
DBMS_SCHEDULER.set_job_argument_value ('myjob', 1, '/q');
DBMS_SCHEDULER.set_job_argument_value ('myjob', 2, '/c');
DBMS_SCHEDULER.set_job_argument_value ('myjob', 3, 'c:\temp\test.bat');
DBMS_SCHEDULER.enable ('myjob');
END;
I'm using Oracle 11g.
What you can do is create a table with one column which will hold the value for scheduler execution. Put all the PL/SQL code in a if-else which checks for the value in this table. If the value is true then the code will execute as required, if the value is false then it just completes the execution without doing anything. So when your batch script starts update the value in the table as false so every time the scheduler runs it will see false and exit without doing anything, at the end of batch script update the value to true so the next scheduler run will execute as required.
Hope it helps.

Oracle job to run daily in Oracle Express edition 11g

![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

How can I track the status of my running job using Oracle's DBMS Scheduler?

Is this possible with oracle's scheduler. I just want to track where it currently is executing, when the job is running and get feedback.
dbms_scheduler.create_job(
job_name => 'hello_oracle_scheduler',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN DBMS_OUTPUT.PUT_LINE('' ''); DBMS_OUTPUT.PUT_LINE(''Hello world of scheduler. Time to execute scheduled jobs!!!''); END;',
number_of_arguments => 0
You better use a table and insert/updates on it to track your JOBs. DMBS_OUTPUT package makes sense in the weird cases where you have a console.
I would recommend using Pablo / Shannon's approach of a table insert through a proc with pragma autonomous_transaction option. However, another option would be to use UTL_MAIL (or UTL_SMTP if on 9i or less) to send an email to yourself if this is just a quick and dirty need.

Resources