I have a list table with a list of foreign key. Therefore, if I want to insert a new object, the missing section throws an exception when pasting. I thought I was a cool duke and use a trigger to create new partitions :-) But the partition will not be available at runtime. If you wait a bit, everything works fine (but dbms_lock.sleep will not do this trick).
So, this is my trigger (and the necessary procedure) - pay attention to the section "check section section" at the end
CREATE OR REPLACE PROCEDURE Execute_DDL
(i_sql IN VARCHAR2)
AS
pragma autonomous_transaction;
BEGIN
EXECUTE IMMEDIATE (i_sql);
commit;
END;
/
.
CREATE OR REPLACE TRIGGER Q_FLD_NEW_PART_TRG
AFTER INSERT
ON Q_FLD
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
l_cnt number;
l_wait_cnt number := 0;
l_alter varchar2(1000);
l_job_stmt varchar2(1000);
l_job_nr number;
l_job dba_jobs_running%rowtype;
BEGIN
SELECT count(*) INTO l_cnt from user_tables
where table_name = 'QUOTE' and partitioned = 'YES';
if l_cnt <= 0 then return; end if;
l_alter := 'ALTER TABLE QUOTE ADD PARTITION QUOTE_PART_' || :new.name || ' VALUES (' || :new.id || ')';
l_job_stmt := 'begin Execute_DDL (''' || l_alter || '''); end;';
DBMS_JOB.SUBMIT (job => l_job_nr, what => l_job_stmt);
if l_job_nr is null then
raise_application_error(-20000, 'Partition Job Creation failed!', true);
end if;
-- wait for job to complete
while l_job_nr is not null loop
l_wait_cnt := l_wait_cnt +1;
if l_wait_cnt > 30 then raise_application_error(-20000, 'pratition creation timed out!'); end if;
begin
select * into l_job from dba_jobs_running where job = l_job_nr;
if l_job.failures >0 then
raise_application_error(-20000, l_job_stmt, true);
end if;
sys.dbms_lock.sleep(2);
exception when no_data_found then
l_job_nr := null; -- job completed
end;
end loop;
-- check partition available
exception when others then
raise_application_error(-20000, l_job_stmt, true);
END q_fld_new_part_trg;
/
Any idea to get around this? I am using 11gR2 64 bit on Linux
Chris source
share