Oracle trigger to queue/dequeue doesn't fire - oracle

Oracle 11g.
I have a trigger which calls a procedure. That procedure performs queue and dequeue.
When I run the procedure in sqldeveloper, it runs fine.
If I update the tables which the trigger fires off, the procedure runs fine.
If I go into the application in question and run a process which causes the trigger to fire, the procedure doesn't work. Specifically the queue and dequeue don't work because if I remove the queue/dequeue code, and instead do an insert on a table, the procedure runs fine.
So I'm thinking that maybe there's a permissions problem causing the trigger unable to execute on dbms_aq. Does anyone know how to resolve this? Is there a user account that runs the triggers which needs to be given privileges?
I don't think the problem is with the procedure because it runs fine. I'm also not passing any dynamic data from the trigger so the way I run the procedure under sqldeveloper is exactly the same way the trigger is calling the procedure.
Note: sanitized code
create or replace
TRIGGER mytrig
AFTER INSERT ON INVENTORY_TRANSACTION
FOR EACH ROW
WHEN ((NEW.CLIENT = 'abcwidgets') AND (NEW.CODE = 'Receipt'))
BEGIN
dschema.mypack.queue_receipt('abcwidgets','999','bbb','1','88');
EXCEPTION
WHEN OTHERS THEN NULL;
END;
PROCEDURE QUEUE_RECEIPT(
CLIENTID IN VARCHAR2
, RECEIPTID IN VARCHAR2
, SITEID IN VARCHAR2
, LINEID IN VARCHAR2
, UPDATE_QTY IN VARCHAR2
) AS
ctxHandle dbms_xmlgen.ctxHandle;
l_xml xmltype;
queueopts DBMS_AQ.ENQUEUE_OPTIONS_T;
msgprops DBMS_AQ.MESSAGE_PROPERTIES_T;
msgid RAW(16);
BEGIN
ctxHandle := dbms_xmlgen.newContext('SELECT line_id,
user_data_4
FROM inventory
');
l_xml := xmltype(dbms_xmlgen.getxml(ctxHandle));
DBMS_AQ.ENQUEUE ('DSCHEMA.ABCWIDGETS_QUEUE',
queueopts,
msgprops,
l_xml,
msgid);
END QUEUE_RECEIPT;

You probably need to fully qualify the QUEUE NAME. The queue name namespace is determined at runtime, and with no public synonym on queues, it will look in the logged in user namespace first, regardless of the procedure owner. If I had to guess, when you login to run it in sqldeveloper, it is probably as the user that owns the procedure you are calling.

The problem was explained to me by a dba which was a bit over my head. There was some variable declarations in a package I was using that were declared outside the scope of a body. I moved these back into the package bodies and this caused them to start working.

Related

How to transfer pop up window functionality into code in Oracle SQL for variable declaration?

I am preparing Oracle SQL program for my team to use. This can eventually used by other teams as well by changing few where conditions. We all are using Toad for Oracle to run the query. So I added variables in the query. See the example code below.
DECLARE v_lob VARCHAR(2);BEGIN v_lob := 't' ;END;
select :v_lob as Test from dual
My issue is when Toad has a pop-up window option to bind the variable. Since my team is using v_lob := 't' any way, I prefer them not to have enter it each time when they query. How can I remove the pop-up window option and use the value 't' for the variable as in the code?
You might want to use a CONTEXT for holding a large number of session variables. This solution should work with any IDE.
One user, one time, must create the context and create a package for setting the context values (based on this oracle-base article):
--Create a package on a shared schema.
create or replace package context_api is
procedure set_parameter(p_name varchar2, p_value varchar2);
end;
/
create or replace package body context_api is
procedure set_parameter(p_name varchar2, p_value varchar2) is
begin
dbms_session.set_context('my_context', p_name, p_value);
end;
end;
/
--Create a context on a shared database.
--(Note that contexts are global objects and don't belong to a specific schema.)
create or replace context my_context using context_api;
Each user then needs code like this in their worksheets:
--At the top of the worksheet, set the variables like this:
begin
context_api.set_parameter('v_lob', 'asdf');
end;
/
--Call the variables with SYS_CONTEXT:
select sys_context('my_context', 'v_lob') as test from dual;
TEST
----
asdf

Using a pl/sql procedure to log errors and handle exceptions

so far stack overflow and the oracle forums and docs have been my best friend in learning PLSQL. I'm running into an issue here. Any advice is appreciated. I'm writing a procedure that would be used to log any errors a package may encounter and log them into the error log table I created. here is my code thus far.
CREATE OR REPLACE PROCEDURE APMS.test_procedure AS
procedure write_error_log (errcode number, errstr varchar2, errline varchar2) is
pragma autonomous_transaction;
-- this procedure stays in its own new private transaction
begin
INSERT INTO error_log
(ora_err_tmsp,
ora_err_number,
ora_err_msg,
ora_err_line_no)
values (CURRENT_TIMESTAMP,
errcode,
errstr,
errline);
COMMIT; -- this commit does not interfere with the caller's transaction.
end write_error_log;
BEGIN
INSERT INTO mockdata
VALUES ('data1', 'mockname', 'mockcity');
exception when others then
write_error_log(sqlcode,sqlerrm,dbms_utility.format_error_backtrace);
raise;
END test_procedure;
/
In the procedure I currently am using a mockdata table to induce an invalid number error and log that to the error_log table. At this point the error log table proves to be functional and inserts the data needed. The next step for me is to use this procedure to be used in the exception handlers in other programs so that the error is caught and logged to the table. Currently, my procedure is only unique to the mock_data table. My mentor/superior is telling me I need to pass this program some parameters to use it in other packages and exception handlers. I'm just having a bit of trouble. Any help would be appreciated thank you!
Steven Feuerstein has written several articles in Oracle Magazine on how to handle errors in PLSQL. He offers a small framework (errpkg) for doing this. The DRY principle (Don't Repeat Yourself) is his mantra!
https://resources.oreilly.com/examples/0636920024859/blob/master/errpkg.pkg
First, you should not make your caller pass in errline. That's very tedious! And what happens when the developer needs to insert a line or two of code? Do they need to update every call to write_error_log after that point to update the line numbers? Your write_error_log should use dbms_utility.format_call_stack (or the 12c more convenient variant of that.. don't have its name handy) to figure out what line of code issued the call to write_error_log.
Then, you should have a 2nd procedure called, say, write_exception. All that needs to do is something like this:
write_error_log (SQLCODE, SUBSTR (DBMS_UTILITY.format_error_stack || DBMS_UTILITY.format_error_backtrace, 1, 4000));

How do I pass a cursor value when calling a stored procedure?

This is only my second time diagnosing a PL/SQL procedure. I need to test the code in the stored procedure, and I'm trying to call it in SQL Developer. I have run the error details report, and the code has no obvious bugs in it.
So now I am trying to run it through a test window so I can see if the output is correct. However I can't seem to get the right argument for the 3 parameter. Here are the parameters in the the procedure.
CREATE OR REPLACE PROCEDURE ADVANCE.WW_DEATHDATE_REPORT(begindate varchar2, enddatevarchar2, RC1 IN OUT du_refCUR_PKG.RC) AS
Here is the Code I am trying to use to call the procedure. What do I need to do to get it to run correct? I keep getting error messages saying I'm using a wrong value in the parameter.
BEGIN
ADVANCE.WW_DEATHDATE_REPORT('20100101','20150101',du_refcur_pkg);
END;
There are multiple ways to do this, one way is like the below,
DECLARE
du_refcur_pkg SYS_REFCURSOR;
BEGIN
OPEN du_refcur_pkg FOR SELECT ... ;
ADVANCE.WW_DEATHDATE_REPORT('20100101','20150101',du_refcur_pkg);
END;
Another way would be,
BEGIN
ADVANCE.WW_DEATHDATE_REPORT( '20100101','20150101', CURSOR (SELECT ... ) );
END;

Calling an Oracle procedure from Java - what happens when System.exit() is called?

Here is code for an Oracle procedure to be called from Java:
CallableStatement st = connection.prepareCall("{call PROCEDURE_0(?,?,?)}");
st.setInt(1,xyz1);
st.setString(2,xyz2);
st.setInt(3,int_variable);
st.registerOutParameter(3,Types.INTEGER);
try{
st.execute();
}
catch(Exception e){
}
If a user calls System.exit() on this Java JVM - what happens to the Oracle procedure that was called? Is it guaranteed to keep running in Oracle? Or perhaps, in order to guarantee this, I should submit it to the Oracle job scheduler instead? So far, my experience has been that sometimes the procedure keeps running even if the JVM terminates, and sometimes it does not. Does this have anything to do with 'registering an out parameter'? Will the code reach the catch block if System.exit is called?
Does anyone have experience with this?
This code which I stole from Mike McAllister which he also stole from an Oracle forum solves the problem I was mentioning in this question. It works, and you can pass dynamic variables as parameters, not just hard-coded values.
this SO question also addresses the problem:
Passing arguments to oracle stored procedure through scheduler job
-- create a stored procedure with two arguments
create or replace procedure myproc (arg1 in varchar2, arg2 in varchar2)
is BEGIN null; END;
/
-- create a program with two arguments and define both
begin
dbms_scheduler.create_program
(
program_name=>'myprog',
program_action=>'myproc',
program_type=>'STORED_PROCEDURE',
number_of_arguments=>2, enabled=>FALSE
) ;
dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>1,
argument_type=>'VARCHAR2',
DEFAULT_VALUE=>'13');
dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>2,
argument_type=>'VARCHAR2');
dbms_scheduler.enable('myprog');
end;
/
-- create a job pointing to a program and set both argument values
begin
dbms_scheduler.create_job('myjob',program_name=>'myprog');
dbms_scheduler.set_job_argument_value('myjob',1,'first arg');
dbms_scheduler.set_job_argument_value('myjob',2,'second arg');
dbms_scheduler.enable('myjob');
end;
/

oracle plsql: retrieve runtime parameter values when you call a procedure

I need a generalized method to get list of runtime parameters (values) when I call a procedure. I need something similar to the $$PLSQL_UNIT that returns the name of the running procedure.
(plsql Oracle 10g)
E.g. look at this sample procedure:
(it simply prints its own name and parameters )
CREATE OR REPLACE PROCEDURE MY_PROC(ow in varchar2, tn IN varchar2)
IS
BEGIN
dbms_output.put_line('proc_name: '||$$PLSQL_UNIT||' parameters: '|| ow||' '||tn );
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERRORE: ' ||SQLERRM);
END MY_PROC;
/
Running procedure produces the following output:
SQL>
1 BEGIN
2 IBAD_OWN.MY_PROC('first_par', 'second_par');
3 END;
4 /
proc_name: MY_PROC parameters: first_par second_par
PL/SQL procedure successfully completed.
I'm not satisfy because I can't copy and paste in all my procedures because I have to hard code each procedure to set their right parameter variables.
Thanks in advance for the help.
It isn't possible to dynamically retrieve the values of parameters passed to a procedure in Oracle PL/SQL. The language simply isn't designed to handle this kind of operation.
Incidentally, in a procedure that is located within a package, $$PLSQL_UNIT will only return the package's name. I find it's better to define a consistently-named constant within each procedure that contains the procedure's name.
When I wanted the same functionality as yours I didn't find any good built-in solution.
What I did is: wrote DB-level trigger which modifies original body of function/procedure/package.
This trigger adds immediatly after "begin" dynamically generated piece of code from "user_arguments".
Plus, after that I include into this trigger the code, that logs calls of procs when exception occures.
Plus, you can trace procs calls, and many more interisting things.
But this solution works fine only for preproduction because performance decreases dramatically.
PS. Sorry for my bad English.

Resources