DBMS_SCHEDULER作业“cleanup_job”如下所示。
DECLARE
stmt VARCHAR2(4000) := '
BEGIN
DELETE FROM useraccounts WHERE accountcreatedate < trunc(sysdate) - 90;
DELETE FROM usertasks WHERE userid NOT IN (SELECT userid FROM useraccounts u where u.userid=userid);
BEGIN
EXECUTE IMMEDIATE ''ALTER TABLE usertasks ADD CONSTRAINT FK_Useraccounts FOREIGN KEY (userid) REFERENCES useraccounts(userid) ON DELETE CASCADE'';
EXCEPTION WHEN OTHERS THEN
IF SQLCODE = -02275 THEN
/*ORA-02275: such a referential constraint already exist*/
DBMS_OUTPUT.PUT_LINE(''Foreign Key : FK_Useraccounts on Delete Cascade in table usertasks already exists'');
ELSE
RAISE;
END IF;
END
END;';
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'cleanup_job',
job_type => 'PLSQL_BLOCK',
job_action => stmt,
start_date => sysdate,
auto_drop => true,
comments => 'Job to cleanup user accounts whose creation date > 90 days',
enabled => TRUE
);
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -27477 THEN
DBMS_OUTPUT.PUT_LINE('Skipped job creation - a cleanup_job already exists for this service.');
ELSE
RAISE;
END IF;
END;
如果我在单个PLSQLBlock中给予所有SQL语句,
stmt varchar(4000) ='
BEGIN
---All SQL Statements here----
END;'
但是,如果我有嵌套PL SQL块,则在运行作业时没有错误,但语句没有执行(在关联的表中看不到任何更改)。
stmt varchar(4000) ='
BEGIN
---SQL Statements----
BEGIN
----SQL Statements-----
END;
END;'
是否不允许将嵌套PL/SQL块分配给变量?
我对这个很陌生,任何帮助都将不胜感激。
1条答案
按热度按时间nlejzf6q1#
是否不允许将嵌套PL/SQL块分配给变量?
这是允许的;您可以在块中嵌套块而不会出现问题。
以及块内块内块等工程。
See fiddle
我在关联的表中没有看到任何更改
无论查询嵌套在多少个块中,它们都可以在上面的小提琴中工作。
SELECT userid FROM useraccounts u WHERE u.userid = userid
将从本地作用域而不是任何外部作用域获取userid
,所以它与SELECT userid FROM useraccounts WHERE userid IS NOT NULL
相同。*