Execute immediate within a DBMS_JOB

Question : Execute immediate within a  DBMS_JOB

Can I do a dynamic SQL WITHIN A DBMS_JOB.  Here is how my job looks like.
————————————————————————————————————
DECLARE
X NUMBER;
viewDDL VARCHAR2(30000);
BEGIN
viewDDL := myPkg.getViewDDL;
SYS.DBMS_JOB.SUBMIT
( job       => X
,what      => viewDDL
,next_date => to_date(’24/10/2008 10:50:38′,’dd/mm/yyyy hh24:mi:ss’)
,interval  => ‘SYSDATE+1/1440 ‘
,no_parse  => TRUE
);
SYS.DBMS_OUTPUT.PUT_LINE(‘Job Number is: ‘ || to_char(x));
END;
/
——————————————————————————————————————–

The viewDDL is dynamic and can change from time to time. That’s why it is in a package and myPkg.getViewDDL returns the CREATE OR REPLACE VIEW statement.
Any ideas why this is not working?


 

Solution: Execute immediate within a  DBMS_JOB

Hi!

This seems to be a rights restrictions problem. In dbms_job, only rights directly granted to the user who created the jobs are available, but not jobs granted through roles (you might find an error message regarding this in the database’s alert.log – file).

However, according to

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:24065646637395

you can avoid the rights issues with the command

execute immediate ‘set role ALL’;

before the create view-command.

So the following should work:

DECLARE

X NUMBER;

BEGIN

SYS.DBMS_JOB.SUBMIT

( job => X

,what => ‘BEGIN execute immediate ”set role ALL”; EXECUTE IMMEDIATE myPkg.getViewDDL; END;’

,next_date => to_date(’27/10/2008 00:00:00′,’dd/mm/yyyy hh24:mi:ss’)

,interval => ‘SYSDATE+1/1440 ‘

,no_parse => TRUE

);

SYS.DBMS_OUTPUT.PUT_LINE(‘Job Number is: ‘ || to_char(x));

END;

/

commit;