I am using DBMS_JOB. Is it possible to pass jobId (which is an OUT parameter from the submit method) as a parameter of the calling procedure?
This is what I am trying:
jobno NUMBER;
sql_string:= 'BEGIN BPM_API_BATCH.' || l_procedure_name || '(:jobno, sysdate); END;';
DBMS_JOB.SUBMIT (jobno,
sql_string,
sysdate,
null);
Do you really need to pass the job number in as an argument?
Within the job, you can call SYS_CONTEXT( 'USERENV', 'BG_JOB_ID' ) to get the job_id without needing to pass it in as a parameter (that will return NULL if the procedure is not called in a job.
A DBMS_JOB.WHAT procedure can be used to change the PL/SQL to run:
http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_job.htm#i1000977
CREATE TABLE testt(
val varchar2(100)
);
DECLARE
jobno NUMBER;
BEGIN
DBMS_JOB.SUBMIT(
job => jobno,
what => 'BEGIN NULL; END;',
NEXT_DATE => sysdate
);
DBMS_JOB.WHAT(
job => jobno,
what => 'BEGIN INSERT INTO testt VALUES(''jobno = ' || jobno || ''' ); commit; end;'
);
commit;
END;
/
SELECT * FROM testt;
VAL
-------------
jobno = 26
Related
I want to call main procedure from the wrapper procedure. Wrapper procedure I'm able to output with dbms output print line but I'm unable to print within the main proc, looks like the main proc is not called from wrapper procedure. Please help me
Below is the table script
CREATE TABLE ASYNC_SAMPLE_TAB
( attribute1 varchar2(50),
attribute2 varchar2(50)
);
Below is my package specification
CREATE OR REPLACE PACKAGE XX_ASYNC_DEMO_PKG
IS
PROCEDURE MAIN_PROC(
attribute1 IN VARCHAR2,
attribute2 IN VARCHAR2
);
PROCEDURE WRAPPER_MAIN_PROC(
attribute1 IN VARCHAR2,
attribute2 IN VARCHAR2
);
END XX_ASYNC_DEMO_PKG;
Below is my package bofy
CREATE OR REPLACE PACKAGE BODY XX_ASYNC_DEMO_PKG
IS
PROCEDURE WRAPPER_MAIN_PROC(
attribute1 IN VARCHAR2,
attribute2 IN VARCHAR2
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('-- START OF WRAPPER PROC---' || SYSTIMESTAMP);
dbms_scheduler.create_job(
job_name => 'ASYNC_MAIN_PROC' || '_' || attribute1,
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN
MAIN_PROC(''' || attribute1 || ''', ''' || attribute2 || ''');
END;',
start_date => systimestamp ,
auto_drop => true,
enabled => true
);
DBMS_OUTPUT.PUT_LINE('-- END OF WRAPPER PROC---' || SYSTIMESTAMP);
END;
PROCEDURE MAIN_PROC(
attribute1 IN VARCHAR2,
attribute2 IN VARCHAR2
)
AS
sql_stmt VARCHAR2(200);
BEGIN
DBMS_OUTPUT.PUT_LINE('-- START OF MAIN PROC---' || SYSTIMESTAMP);
DBMS_SESSION.sleep(10);
sql_stmt := 'INSERT INTO ASYNC_SAMPLE_TAB VALUES (:1, :2)';
EXECUTE IMMEDIATE sql_stmt USING attribute1, attribute2;
DBMS_OUTPUT.PUT_LINE('-- END OF MAIN PROC---' || SYSTIMESTAMP);
END;
END XX_ASYNC_DEMO_PKG;
Below is the command with which I'm trying to test above solution
exec XX_ASYNC_DEMO_PKG.WRAPPER_MAIN_PROC('Test101_1', 'Test101_2');
As described in the documentation, the main purpose of DBMS_OUTPUT is debug messages to trace execution flow. It's also said there that it doesn't immediately return anything to the caller, but text is placed into the buffer and may be retrieved from there by the caller or the program itself (though, you cannot "get into" any other program flow and get anything from the executing code until the called unit is complete).
Scheduler doesn't retrieve the content of the buffer after it completes the job, so DBMS_OUTPUT.PUT* called inside a background job has no effect and you cannot see any result.
You may use some logging table (with separate logging procedure executed in autonomous transaction not to have an influence or be influenced by the main transaction) or external file and UTL_FILE to send results here. Or use a general table that should be processed by the job and check results after it finishes.
UPD:
Below is a complete setup to demonstrate how to pass parameters to a callable and see its results.
We'll log results into this table:
create table log_table (
ts timestamp,
val number,
job_name varchar2(30)
)
Then this procedure will be used to perform an action:
create or replace procedure proc_insert(
p_val in number,
p_job_name in varchar2
) as
begin
insert into log_table(ts, val, job_name)
values (systimestamp, p_val, p_job_name);
commit;
end;
This procedure will call our action procedure in a background job. We need to specify a number of parameters to override default values in a callable when creating a job. And then use DBMS_SCHEDULER.SET_JOB_[ARGUMENT|ANYDATA]_VALUE depending on the parameter type.
create or replace procedure proc_insert_async(
p_call_id varchar2,
p_val number,
r_job_name out varchar2
) as
l_job_prefix constant varchar2(20) := 'LOG_TABLE_';
l_job_name varchar2(100) := l_job_prefix || p_call_id;
begin
dbms_scheduler.create_job(
job_name => l_job_name,
job_type => 'STORED_PROCEDURE',
/*Callable unit - out API procedure*/
job_action => 'TEST_EAS.PROC_INSERT',
/*Number of args to be passed to the proc*/
number_of_arguments => 2,
/*Run immediately*/
start_date => sysdate,
enabled => false,
auto_drop => true
);
/*Pass parameters to the callable*/
dbms_scheduler.set_job_anydata_value (
job_name => l_job_name,
argument_position => 1,
argument_value => sys.anydata.convertNumber(p_val)
);
dbms_scheduler.set_job_argument_value(
job_name => l_job_name,
argument_position => 2,
argument_value => l_job_name
);
/*Start job*/
dbms_scheduler.enable(
name => l_job_name
);
r_job_name := l_job_name;
end;
Finally, test this in action (sleep is added due to async nature of inserts to wait for results).
declare
l_job_name varchar2(100);
begin
proc_insert_async(
p_call_id => 'test1',
p_val => 1,
r_job_name => l_job_name
);
dbms_output.put_line(l_job_name);
proc_insert_async(
p_call_id => 'test2',
p_val => 10,
r_job_name => l_job_name
);
dbms_output.put_line(l_job_name);
dbms_session.sleep(3);
end;
/
LOG_TABLE_test1
LOG_TABLE_test2
PL/SQL procedure successfully completed.
select *
from log_table
TS
VAL
JOB_NAME
25.11.22 10:52:37,310403000
10
LOG_TABLE_test2
25.11.22 10:52:37,302992000
1
LOG_TABLE_test1
I want to create a export table job, but I can't understand why its not working.
my table is Department
create table department (id number, name varchar2(200));
I want to export a csv file for per day at 9:00 pm. I need to create it.
I only know:
0. create a directory
create a PROCEDURE
create a DBMS_SCHEDULER.CREATE_PROGRAM
create a DBMS_SCHEDULER.CREATE_SCHEDULE
create a DBMS_SCHEDULER.CREATE_JOB
excute the job
thanks
Yes, you can use DBMS_SCHEDULER by creating in such a way
DECLARE
v_job_name VARCHAR2(32) := 'jb_exp_emp_data';
BEGIN
DBMS_SCHEDULER.CREATE_JOB(job_name => v_job_name,
job_type => 'STORED_PROCEDURE',
job_action => 'exp_emp_data',
start_date => TO_DATE('11-12-2021 21:00:10',
'DD-MM-YYYY HH24:MI:SS'),
repeat_interval => 'FREQ=DAILY; BYHOUR=21;',
auto_drop => false,
comments => 'Exports the content of the department table every day at 9:00PM o''clock ');
DBMS_SCHEDULER.ENABLE(v_job_name);
END;
/
that starts at the time defined by the start_date parameter, then repeats on every upcoming days at 9pm in the future.
I followed the steps below and it was successful ...
Create a directory (path of export file):
CREATE OR REPLACE DIRECTORY CSVDIR AS 'D:\';
Create a procedure:
Create Or Replace Procedure exp_emp_data Is
today varchar2(200);
fileName varchar2(200);
n_file utl_file.file_type;
v_string Varchar2(4000);
Cursor c_emp Is
Select
id, name
From
department;
Begin
select to_char(sysdate,'yyyymmdd','nls_calendar=persian') into today from dual;
fileName := 'empdata' || today || '.csv';
n_file := utl_file.fopen('CSVDIR', fileName, 'w', 4000);
v_string := 'ID, Name';
utl_file.put_line(n_file, v_string);
-- open the cursor and concatenate fields using comma
For cur In c_emp Loop
v_string := cur.id
|| ','
|| cur.name;
-- write each row
utl_file.put_line(n_file, v_string);
End Loop;
-- close the file
utl_file.fclose(n_file);
Exception
When Others Then
-- on error, close the file if open
If utl_file.is_open(n_file) Then
utl_file.fclose(n_file);
End If;
End;
/
-------- Test
Begin
exp_emp_data;
End;
/
Create a program:
BEGIN
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name => 'PROG_EXPORT_TABLE',
program_action => 'exp_emp_data',
program_type => 'STORED_PROCEDURE');
END;
/
Create a job:
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'JOB_EXPORT_TABLE',
job_type => 'STORED_PROCEDURE',
job_action => 'PROG_EXPORT_TABLE',
start_date => '16-nov-2021 11:50:00 pm',
repeat_interval => 'FREQ=DAILY;BYHOUR=23;BYMINUTE=59',
enabled => true
);
END;
/
And enabled it:
exec dbms_scheduler.enable('JOB_EXPORT_TABLE');
The below sample code illustrates an issue I am having with dbms_application_info. If I use it in the below procedure:
create or replace procedure test01 is
vsql varchar2(50);
begin
vsql := 'select sysdate from dual';
execute immediate vsql;
DBMS_APPLICATION_INFO.SET_MODULE('TEST','Starting...');
dbms_lock.sleep ( 10 );
DBMS_APPLICATION_INFO.SET_MODULE(NULL, NULL);
end;
/
exec test01;
Then querying v$session reveals "Starting..." as I would hope!
However, it is necessary to run the related procedure in a job. If I do this, then I cannot see "Starting..."
declare
JNAME varchar2(200) := to_char(sysdate, 'YYYYMMDDHHMiSS');
BEGIN
DBMS_SCHEDULER.create_job (
job_name => 'TEST01_'||JNAME,
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN TEST.TEST01; END;',
start_date => NULL,
repeat_interval => NULL,
enabled => TRUE);
END;
/
This code should be executable by anyone who potentially wishes to have a look and perhaps help me understand why this would be?
Thank you! --> Scouse.
You must use the INTO clause in your execute immediate statement
create or replace procedure test01 is
vsql varchar2(50);
l_date DATE;
begin
vsql := 'select sysdate from dual';
execute immediate vsql into l_date; --<<<< here
DBMS_APPLICATION_INFO.SET_MODULE('TEST','Starting...');
dbms_lock.sleep ( 20 );
DBMS_APPLICATION_INFO.SET_MODULE(NULL, NULL);
end;
/
Using INTO clause both module and action are set.
As documented if you ommit the INTO clause no exception is raised but
If dynamic_sql_statement is a SELECT statement, and you omit both into_clause and bulk_collect_into_clause, then execute_immediate_statement never executes.
So I'd expect the application info should be set even without INTO clause, but from some reason it is not. Anyway using INTO it works fine.
I tested on 12.1
I want to create one PL/SQL block where try to run the job using dbms_scheduler package and I want to gather schema stats of all 30 schema. Eg:
begin
dbms_scheduler_create_job(
job_name => ....,
job_type = > 'PL/SQL BLOCK',
job_action => 'declare
sch_lst dbms_stats.objecttab := dbms_stats.objecttab()
begin
sch_lst.extend(10);
sch_lst(1).ownname := "ab"; --ab is the Schema name
sch_lst(2).ownname := "cd";
.........
sch_lst(30).ownname := "xy";
dbms_stats.gather_schema_stats( ......)
end;
/
',
start_date => sysdate,
..........);
end;
/
Before start_date => sysdate, remove / and also in the schema name instead of " (double quote) use '' (double single quote) because it is inside the declare statement which is already inside single quote.
begin
dbms_scheduler_create_job(
job_name => ....,
job_type = > 'PL/SQL BLOCK',
job_action => 'declare
sch_lst dbms_stats.objecttab := dbms_stats.objecttab()
begin
sch_lst.extend(10);
sch_lst(1).ownname := ''ab''; --ab is the Schema name
sch_lst(2).ownname := ''cd'';
.........
sch_lst(10).ownname := ''kl'';
dbms_stats.gather_schema_stats( ......)
end;
',
start_date => sysdate,
..........);
end;
/
Then after compile this one. you can check the job by using execute dbms_schedule.run_job('<job_name>');
Firstly, you may create such a procedure :
create or replace procedure pr_schema_stats is
sch_lst owa.vc_arr;
begin
sch_lst(1) := 'ab';
sch_lst(2) := 'cd';
sch_lst(3) := 'ef';
sch_lst(4) := 'gh';
sch_lst(5) := 'ij';
sch_lst(6) := 'kl';
sch_lst(7) := 'mn';
sch_lst(8) := 'op';
sch_lst(9) := 'rs';
sch_lst(10):= 'tu';
for i in 1..10
loop
dbms_stats.gather_schema_stats(upper(sch_lst(i)),degree => 4, cascade => true );
end loop;
end;
and then call from scheduler as :
declare
v_job_name varchar2(70) := 'jb_gather_stats';
begin
dbms_scheduler.create_job(
job_name => v_job_name,
job_type => 'STORED_PROCEDURE',
job_action => 'pr_schema_stats',
start_date => to_date('04-12-2018 19:00:00', 'dd-mm-yyyy hh24:mi:ss'),
repeat_interval => 'FREQ=MONTHLY;INTERVAL=1;',
auto_drop => false,
comments => 'Produces statistics for Cost based SQL statements');
dbms_scheduler.enable(v_job_name);
end;
EDIT : You can replace your procedure's code with :
create or replace procedure pr_schema_stats is
begin
for c in (
select u.username,
row_number() over (order by u.username) as rn
from dba_users u
where u.account_status = 'OPEN'
and u.username not like 'SYS%'
)
loop
begin
dbms_stats.gather_schema_stats(c.username,degree => 4, cascade => true );
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
to include all of the ordinary schemas in the analyze task.
I have a pl/sql query and I want it's output to be sent in email in CSV format straightaway. I have no directory to first create and save a CSV file and then pick it up to send as an attachment.
Please help with your inputs as I am not able to get away.
Regards,
Sachin
Finally figured out a solution with the help of pointers received and providing the same to further help in case someone else needs in future.
My problem was that I was mostly seeing the examples where i could either save the file on a directory or pick the file from a directory to send as an attchment but I had no provision of directory and I wanted query result to be put in CSV and sent in email dynamically. So here is the complete solution.
CREATE OR REPLACE PROCEDURE SEND_CSV_ATTACHMENT AS
v_sender VARCHAR2(130);
v_recipients VARCHAR2(4000);
v_cc VARCHAR2(4000);
v_bcc VARCHAR2(2000);
v_subj VARCHAR2(200);
v_msg CLOB;
v_mime VARCHAR2(40);
v_tbl VARCHAR2(20000);
c_cr_lf CONSTANT CHAR (2) := (CHR (13) || CHR (10)); -- Carriage Return/Line Feed characters for formatting text emails
v_loop_count PLS_INTEGER := 0;
v_attachment CLOB;
v_block_qry VARCHAR2(3000);
v_block_row VARCHAR2(6000);
TYPE bl_cur IS REF CURSOR;
v_result bl_cur;
v_rowcount NUMBER;
errMsg VARCHAR2(15000);
BEGIN
v_sender := 'somesender#xyzcommunications.com';
SELECT NVL(EMAIL_LIST, 'someone#abcd.com')
FROM
(
SELECT LISTAGG(EMAIL_ID, ',') WITHIN GROUP (ORDER BY EMAIL_ID) AS EMAIL_LIST FROM RECIPEINTS_TABLE WHERE SEND_TO = 1 AND IS_ACTIVE = 1
);
SELECT NVL(EMAIL_LIST, 'someone#abcd.com')
FROM
(
SELECT LISTAGG(EMAIL_ID, ',') WITHIN GROUP (ORDER BY EMAIL_ID) AS EMAIL_LIST FROM RECIPEINTS_TABLE WHERE SEND_CC = 1 AND IS_ACTIVE = 1
);
v_bcc := 'someone#abcd.com';
-- Generate attachment - Begin
v_attachment := '"COL1", "COL2"' || CHR(13) || CHR(10);
v_block_qry := 'SELECT ''"'' || COL1 || ''", "'' || COL2 || ''"'' AS ROWTXT
FROM MY_TABLE';
OPEN v_result FOR v_block_qry;
LOOP
v_rowcount := v_result%ROWCOUNT;
FETCH v_result INTO v_block_row;
EXIT WHEN v_result%NOTFOUND;
v_attachment := v_attachment || v_block_row || chr(13) || chr(10);
END LOOP;
CLOSE v_result;
-- Generate attachment - End
v_subj:= 'MAIL_SUBJECT ' || TO_CHAR(TRUNC(SYSDATE-1), 'YYYY-MM-DD');
UTL_MAIL.send_attach_varchar2(sender => v_sender,
recipients => v_recipients,
cc => v_cc,
bcc => v_bcc,
subject => v_subj,
message => v_msg,
mime_type => 'text/html; charset=us-ascii', -- send html e-mail
attachment => v_attachment,
att_inline => FALSE,
att_filename => 'Change_Report' || TO_CHAR(TRUNC(SYSDATE-1), 'YYYY-MM-DD') || '.csv');
EXCEPTION
WHEN OTHERS THEN
errMsg := SQLERRM;
SEND_MAIL_HTML ('someone#abcd.com', NULL, NULL, errMsg, 'SEND_MAIL ERROR: ' || errMsg);
END SEND_CSV_ATTACHMENT;
You may create such a procedure :
create or replace procedure prFileSend is
v_mail_owner varchar2(100):='myname#someComp.com';
v_url varchar2(4000);
v_rep varchar2(4000);
delimiter varchar2(1) := chr(38);
begin
for c in ( select * from myTable )
loop
begin
v_url := 'http://www.mycompany.com/einfo/default.aspx?email='||c.email || delimiter || 'p1=' || c.col1 || delimiter ||'p2='||c.col2;
v_rep := utl_http.request(utl_url.escape(v_url, false,'ISO-8859-9'));
end;
end loop;
exception
when others then
prErrorMsgSend(v_mail_owner,'Error : ' || sqlerrm); -- a function like this one which sends an error message back to you.
end;
and create a scheduler job
begin
dbms_scheduler.create_job (
job_name => 'jbFileSend ',
job_type => 'STORED_PROCEDURE',
job_action => 'prFileSend',
start_date => '22-jan-2018 09:00:00 am',
repeat_interval => 'FREQ=DAILY; INTERVAL=1',
comments => 'Sending Every day'
enabled => true);
end;
working every day as an example.