unable to call main procedure from wrapper procedure asynchronously - oracle

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

Related

how to create a export table job in oracle 12c

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');

Job/Procedure to PL/SQL execution?

I've this PL/SQL and is working correctly also email is sended.
BEGIN
FOR cur_rec IN
(select JOB, SCHEMA_USER, WHAT from dba_jobs where Broken = 'Y') LOOP
BEGIN
SCHEMA2.send_mail(
p_to => 'receive#test.com',
p_from => 'send#test.com',
p_subject => 'JOB Report',
p_message => 'Job name is: ' || cur_rec.what,
p_smtp_host => 'webmail.test.com');
END;
END LOOP;
END;
/
I need to schedule that PL/SQL execution every 4 hours, But I don't know how to create a job or a procedure with that code, I've tried a lot but still saying:
Completed with warnings
Any help to construct the job or/and procedure is appreciated.
begin
DBMS_SCHEDULER.CREATE_JOB (
job_name=>'my_job',
job_type=>'PLSQL_BLOCK',
job_action=>
'BEGIN
FOR cur_rec IN
(select JOB, SCHEMA_USER, WHAT from dba_jobs where Broken = ''Y'') LOOP
BEGIN
SCHEMA2.send_mail(
p_to => ''receive#test.com'',
p_from => ''send#test.com'',
p_subject => ''JOB Report'',
p_message => ''Job name is: '' || cur_rec.what,
p_smtp_host => ''webmail.test.com'');
END;
END LOOP;
END;',
start_date=>sysdate+1, --start tomorrow at this time
repeat_interval=>'FREQ=HOURLY; INTERVAL=4', --repeat every 4 hours
auto_drop=>false
);
end;
/
Review documentation for additional options.. create job

advanced queuing for table replication to other database

So, I am trying to replicate the data in tables to another database using Advanced Queuing. I created a table on both databases:
create table test
(
id number(10) primary key,
text varchar2(100)
);
then I created the queue
create type table_repli_payload_type AS OBJECT
( rowid_record varchar2(100)
, tabelle VARCHAR2(255)
, schema VARCHAR2(50)
);
begin
SYS.DBMS_AQADM.create_queue_table(queue_table => 'table_repli_queue_table',
queue_payload_type => 'table_repli_payload_type',
multiple_consumers => TRUE);
SYS.DBMS_AQADM.CREATE_QUEUE (
queue_name => 'table_repli_queue',
queue_table => 'table_repli_queue_table'
);
SYS.DBMS_AQADM.START_QUEUE (
queue_name => 'table_repli_queue'
);
end;
wrote a procedure for the replication
create or replace procedure table_repli_callback(
context RAW,
reginfo SYS.AQ$_REG_INFO,
descr SYS.AQ$_DESCRIPTOR,
payload RAW,
payloadl NUMBER
) AS
r_dequeue_options DBMS_AQ.DEQUEUE_OPTIONS_T;
r_message_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
v_message_handle RAW(16);
o_payload table_repli_payload_type;
v_error varchar2(4000);
BEGIN
insert into log_table values(sysdate, 'start table_repli_callback');
r_dequeue_options.msgid := descr.msg_id;
r_dequeue_options.consumer_name := descr.consumer_name;
DBMS_AQ.DEQUEUE(
queue_name => descr.queue_name,
dequeue_options => r_dequeue_options,
message_properties => r_message_properties,
payload => o_payload,
msgid => v_message_handle
);
insert into log_table values(sysdate, 'ROWID: '||o_payload.rowid_record);
merge into test#test_link a
using (select * from test where rowid=o_payload.rowid_record) b on (a.id=b.id)
when matched then
update set text=b.text
when not matched then
insert values(b.id, b.text);
COMMIT;
exception
when others then
v_error:=sqlerrm;
insert into log_table values(sysdate, 'ERROR: '||v_error);
commit;
END;
/
and subscribed it
BEGIN
DBMS_AQADM.ADD_SUBSCRIBER (
queue_name => 'table_repli_queue',
subscriber => SYS.AQ$_AGENT(
'table_repli_queue_subscriber',
NULL,
NULL )
);
DBMS_AQ.REGISTER (
SYS.AQ$_REG_INFO_LIST(
SYS.AQ$_REG_INFO(
'table_repli_queue:table_repli_queue_subscriber',
DBMS_AQ.NAMESPACE_AQ,
'plsql://table_repli_callback',
HEXTORAW('FF')
)
),
1
);
END;
/
I played around with inserting/updating data in the TEST-table and than executing(with changing ids) this Code
DECLARE
r_enqueue_options DBMS_AQ.ENQUEUE_OPTIONS_T;
r_message_properties DBMS_AQ.MESSAGE_PROPERTIES_T;
v_message_handle RAW(16);
o_payload table_repli_payload_type;
v_rowid_record varchar2(100);
BEGIN
select rowid
into v_rowid_record
from test
where id=2;
o_payload := table_repli_payload_type(
v_rowid_record, '', ''
);
DBMS_AQ.ENQUEUE(
queue_name => 'table_repli_queue',
enqueue_options => r_enqueue_options,
message_properties => r_message_properties,
payload => o_payload,
msgid => v_message_handle
);
COMMIT;
END;
/
It's pretty random if it is working. He always seems to write something into TABLE_REPLI_QUEUE_TABLE, but when it is gone, about half the time there doesn't appear anything in LOG_TABLE and the data in the second database hasn't changed.
The error was a strange behavior in TOAD.
I sometimes write test scripts, with ddl, dml, selects, pl/sql-blocks in it and execute them by placing my cursor in a part of the desired command and press shift+F9. It seems like my TOAD just didn't execute the PL/SQL-block although it told me, it did.
I put the PL/SQL-block in another tab and just hit F9 and it worked fine, every time.

Use Job ID as a procedure parameter

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

scheduled job is not launching

I would like to launch a scheduled job after every two hours starting from 04:15 tomorrow. The job should launch everyday and after every two hours. Something like at : 04:15, 06:15, 08:15....
The procedure is about creating file in a remote machine. When I test the procedure then it works just fine, and creates file in remote location. However, it fails as a job in dbms_jobs package. I am not sure what I am doing wrong. Here is the code of procedure:
CREATE OR REPLACE PROCEDURE ARC_HRVR.VR_AD_INTEGRATION_EXPORT AS
v_file UTL_FILE.file_type;
BEGIN
v_file := UTL_FILE.fopen('DIR_VR_AD_INTEGRATION', 'HRMtoAD1_'||to_char(sysdate,'YYYYMMDD')||'_'||to_char(sysdate,'HH24MISS'), 'w', 32767);
FOR x IN (
SELECT * FROM (SELECT
decode(pid, NULL, RPAD(' ',7,' '), RPAD(user_id, 7, ' '))|| '' ||
decode(o365, NULL, RPAD(' ',80,' '), RPAD(o365, 80, ' '))
str FROM table WHERE integrated = 'N') str WHERE rownum <= 1000 ORDER BY rownum)
´LOOP
BEGIN
UTL_FILE.put_line(v_file, x.str);
END;
END LOOP;
UTL_FILE.fflush(v_file);
UTL_FILE.fclose(v_file);
END VR_AD_INTEGRATION_EXPORT;
And here is the code for launching job:
DECLARE
l_id binary_integer;
begin
dbms_job.submit(job => l_id, what => 'ARC_HRVR.vr_ad_integration_export();', interval => 'TRUNC(SYSDATE)+1+4.25/24', );
dbms_output.put_line(l_id);
end;
A bit of guidance and tweaking will fix my code :-)
Thanks in advance
Are you putting in the job as the procedure owner (ARC_HRVR) or as a user that has access to execute the code?
Have you tried surrounding the 'what' in a execution block?
begin ARC_HRVR.vr_ad_integration_export(); end;
The job is launching, right? You see next_date getting updated in the view dba_jobs, etc?
You can refer this example
BEGIN
DBMS_SCHEDULER.create_job (
job_name => 'test_full_job_definition',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN my_job_procedure; END;',
start_date => SYSTIMESTAMP,
repeat_interval => 'freq=hourly; byminute=0; bysecond=0;',
end_date => NULL,
enabled => TRUE,
comments => 'Job defined entirely by the CREATE JOB procedure.');
END;
/
and for more detail please go through this Scheduler

Resources